Getting started
Install the package:
npm install --save domstatejsxEnabling JSX
Section titled “Enabling JSX”domstatejsx ships a jsx-runtime compatible with automatic JSX
transformation. If you are running a Vite project you
have three options:
1. Use the Vite plugin
Section titled “1. Use the Vite plugin”import domstatejsxPlugin from 'domstatejsx/vite-plugin';
export default { plugins: [domstatejsxPlugin()], // ...};2. Configure esbuild directly
Section titled “2. Configure esbuild directly”export default { esbuild: { jsx: 'automatic', jsxImportSource: 'domstatejsx', }, // ...};3. Use the classic runtime with pragmas
Section titled “3. Use the classic runtime with pragmas”Include this in every .jsx file you want domstatejsx to take over:
import { createElement, Fragment } from 'domstatejsx';/** @jsx createElement *//** @jsxFrag Fragment */Your first component
Section titled “Your first component”document.body.append(<h1>hello world</h1>);JSX expressions return native DOM elements. The above is roughly equivalent to:
const element = document.createElement('h1');element.textContent = 'hello world';document.body.append(element);Components are simply functions that return DOM elements:
function Counter() { return ( <> <div> <button>Click me</button> </div> <div> Count: <span>0</span> </div> </> );}
document.body.append(<Counter />);