-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.js
59 lines (53 loc) · 1.76 KB
/
index.js
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
50
51
52
53
54
55
56
57
58
59
import React, { useTransition } from 'react';
import { createStore } from 'react-hooks-global-state';
import {
syncBlock,
useRegisterIncrementDispatcher,
initialState,
reducer,
ids,
useCheckTearing,
shallowEqual,
} from '../common';
const { GlobalStateProvider, dispatch, useGlobalState } = createStore(reducer, initialState);
const Counter = React.memo(() => {
const [count] = useGlobalState('count');
syncBlock();
return <div className="count">{count}</div>;
}, shallowEqual);
const Main = () => {
const [count] = useGlobalState('count');
useCheckTearing();
useRegisterIncrementDispatcher(React.useCallback(() => {
dispatch({ type: 'increment' });
}, []));
const [localCount, localIncrement] = React.useReducer((c) => c + 1, 0);
const normalIncrement = () => {
dispatch({ type: 'increment' });
};
const [startTransition, isPending] = useTransition();
const transitionIncrement = () => {
startTransition(() => {
dispatch({ type: 'increment' });
});
};
return (
<div>
<button type="button" id="normalIncrement" onClick={normalIncrement}>Increment shared count normally (two clicks to increment one)</button>
<button type="button" id="transitionIncrement" onClick={transitionIncrement}>Increment shared count in transition (two clicks to increment one)</button>
<span id="pending">{isPending && 'Pending...'}</span>
<h1>Shared Count</h1>
{ids.map((id) => <Counter key={id} />)}
<div className="count">{count}</div>
<h1>Local Count</h1>
{localCount}
<button type="button" id="localIncrement" onClick={localIncrement}>Increment local count</button>
</div>
);
};
const App = () => (
<GlobalStateProvider>
<Main />
</GlobalStateProvider>
);
export default App;