Skip to content

Getting started

Install the package:

Terminal window
npm install --save domstatejsx

domstatejsx ships a jsx-runtime compatible with automatic JSX transformation. If you are running a Vite project you have three options:

vite.config.js
import domstatejsxPlugin from 'domstatejsx/vite-plugin';
export default {
plugins: [domstatejsxPlugin()],
// ...
};
vite.config.js
export default {
esbuild: {
jsx: 'automatic',
jsxImportSource: 'domstatejsx',
},
// ...
};

Include this in every .jsx file you want domstatejsx to take over:

import { createElement, Fragment } from 'domstatejsx';
/** @jsx createElement */
/** @jsxFrag Fragment */
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 />);