-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (42 loc) · 1.17 KB
/
Copy pathscript.js
File metadata and controls
49 lines (42 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { h, render } from "//cdn.skypack.dev/preact";
import { useState } from "//cdn.skypack.dev/preact/hooks";
import htm from "//cdn.skypack.dev/htm";
import {
defineComponent,
ref,
computed,
onMounted,
onUnmounted,
} from "//cdn.skypack.dev/reactivue/preact";
const html = htm.bind(h);
const MyCounter = defineComponent(
// setup function in Vue
(props) => {
const counter = ref(props.value);
const doubled = computed(() => counter.value * 2);
const inc = () => (counter.value += 1);
onMounted(() => console.log("Counter mounted"));
onUnmounted(() => console.log("Counter unmounted"));
return { counter, doubled, inc };
},
// functional component in preact
({ counter, doubled, inc }) => {
// you can use preact hooks here
return html`
<div>
<div>${counter} x 2 = ${doubled}</div>
<button onClick=${inc}>Increase</button>
</div>
`;
}
);
function App() {
const [show, setShow] = useState(true);
return html`
<button onClick=${() => setShow(!show)}>
${show ? "Hide" : "Show"} counter
</button>
${show && html`<${MyCounter} value=${10} />`}
`
}
render(html`<${App}/>`, document.body);