Created
February 12, 2025 13:31
-
-
Save robertknight/bb5db8c19a65ea15c44abc9ca94373a0 to your computer and use it in GitHub Desktop.
Preact in vanilla HTML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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