Skip to content

Instantly share code, notes, and snippets.

@robertknight
Created February 12, 2025 13:31
Show Gist options
  • Save robertknight/bb5db8c19a65ea15c44abc9ca94373a0 to your computer and use it in GitHub Desktop.
Save robertknight/bb5db8c19a65ea15c44abc9ca94373a0 to your computer and use it in GitHub Desktop.
Preact in vanilla HTML
<html>
<body>
<div id="app"></div>
<script type="importmap">
{
"imports": {
"preact": "https://unpkg.com/[email protected]/dist/preact.module.js",
"preact/hooks": "https://unpkg.com/[email protected]/hooks/dist/hooks.module.js"
}
}
</script>
<script type="module">
const container = document.querySelector('#app');
import { h, render } from 'preact';
import { useState } from 'preact/hooks';
function Form() {
const [rows, setRows] = useState([]);
return h('form', {},
h('input', { defaultValue: 'initial name' }),
rows.map(row => h('div', {}, row)),
h('button', {
type: 'button',
onClick: () => setRows([...rows, 'new-row']),
}, 'Add row')
);
}
render(h(Form), container);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment