Skip to content

Instantly share code, notes, and snippets.

@fev4
Last active November 28, 2020 00:10
Show Gist options
  • Save fev4/e41f8fe61f0dc1833979efde22b9eb2a to your computer and use it in GitHub Desktop.
Save fev4/e41f8fe61f0dc1833979efde22b9eb2a to your computer and use it in GitHub Desktop.
import { h, render } from 'https://unpkg.com/preact@latest?module';
import {
useState,
useEffect,
} from 'https://unpkg.com/preact@latest/hooks/dist/hooks.module.js?module';
import htm from 'https://unpkg.com/htm@latest/dist/htm.module.js?module';
// Initialize htm with Preact
const html = htm.bind(h);
function Props() {
const [count, setCount] = useState(0);
// set counters
const increment = () => setCount(count + 1);
// You can also pass a callback to the setter
const decrement = () => setCount((currentCount) => currentCount - 1);
return html`
<div>Count: ${count}</div>
<p>New content!</p>
<button onClick="${increment}">Increment</button>
<button onClick="${decrement}">Decrement</button>
`;
}
render(html` <${Props} /> `, document.getElementById('preact-container'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment