-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcounterHookleton.js
More file actions
44 lines (38 loc) · 1.04 KB
/
counterHookleton.js
File metadata and controls
44 lines (38 loc) · 1.04 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
import { useReducer, useEffect } from 'react';
import { createHook } from 'hookleton';
const initialState = { count: 0 };
export const reducer = (state, action) => {
switch (action.type) {
case 'reset':
return initialState;
case 'increment':
return { count: state.count + 1 };
case 'decrement':
return { count: state.count - 1 };
default:
return state;
}
};
const useCounter = createHook(useReducer);
const Counter = () => {
const [store, dispatch] = useCounter.use(reducer, initialState);
return (
<div>
<p>You clicked {store.count} times</p>
<button onClick={() => dispatch({ type: 'increment' })}>+</button>
<button onClick={() => dispatch({ type: 'decrement' })}>-</button>
<button onClick={() => dispatch({ type: 'reset' })}>Reset</button>
</div>
);
};
let c = 0;
const Any = () => {
useEffect(() => () => (c = 0), []);
return <p style={{ color: 'red' }}>I should be rendered ONE time but: {++c}</p>;
};
export default () => (
<>
<Any />
<Counter />
</>
);