-
Notifications
You must be signed in to change notification settings - Fork 45
/
index.js
56 lines (50 loc) · 1.36 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
import React from 'react';
import { createStore } from 'redux';
import { useSubscription } from 'use-subscription';
import {
syncBlock,
useRegisterIncrementDispatcher,
reducer,
ids,
useCheckTearing,
} from '../common';
const store = createStore(reducer);
const Counter = React.memo(() => {
const count = useSubscription(React.useMemo(() => ({
getCurrentValue: () => store.getState().count,
subscribe: (callback) => {
return store.subscribe(callback);
},
}), []));
syncBlock();
return <div className="count">{count}</div>;
});
const Main = () => {
const count = useSubscription(React.useMemo(() => ({
getCurrentValue: () => store.getState().count,
subscribe: (callback) => {
return store.subscribe(callback);
},
}), []));
useCheckTearing();
useRegisterIncrementDispatcher(React.useCallback(() => {
store.dispatch({ type: 'increment' });
}, []));
const [localCount, localIncrement] = React.useReducer(c => c + 1, 0);
return (
<div>
<h1>Remote 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 = () => (
<React.Fragment>
<Main />
</React.Fragment>
);
export default App;