-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcounter.js
More file actions
32 lines (27 loc) · 722 Bytes
/
counter.js
File metadata and controls
32 lines (27 loc) · 722 Bytes
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
import { useState } from 'react';
import { createHook } from 'hookleton';
// useCounter is a useState but global
const useCounter = createHook(useState);
const Increment = () => {
const [, update] = useCounter();
const increment = () => update(s => s + 1);
return <button onClick={increment}>+</button>;
};
const Decrement = () => {
const [, update] = useCounter();
const decrement = () => update(s => s - 1);
return <button onClick={decrement}>-</button>;
};
// The host component
const Value = () => {
const [count] = useCounter.use(0);
return <span>{count}</span>;
};
// Value componet must be at the top
export default () => (
<div>
<Value />
<Increment />
<Decrement />
</div>
);