|
1 | 1 | import { |
2 | | - useEffect, useReducer, useMemo, useRef, |
| 2 | + useEffect, useReducer, useMemo, |
3 | 3 | } from 'react'; |
4 | 4 |
|
5 | | -// useFluxStore essentially combines useReducer and useEffect to use with FluxStores |
| 5 | + |
| 6 | +import isEqual from 'lodash.isequal'; |
| 7 | + |
| 8 | +// useFluxStore combines useReducer and useEffect to use with FluxStores |
6 | 9 | // useReducer: Used to extract relevant values from the store |
7 | 10 | // useEffect is used to attach a listener to the store |
8 | 11 |
|
9 | | -export function useFluxStore(store, reducer, deps = []) { |
10 | | - // Use a ref to calculate reducer's initial value from current state of store. |
11 | | - const initReducer = useRef(); |
12 | 12 |
|
13 | | - if (!initReducer.current) { |
14 | | - initReducer.current = reducer(null, store); |
| 13 | +export function useFluxStore(store, reducer, deps = [], strictEquality = false) { |
| 14 | + // We use Lodash's isEqual check to make sure the state hasn't changed |
| 15 | + // This can be expensive, but cheaper than a re-render |
| 16 | + function reducerWithEqualityCheck(_p, _store) { |
| 17 | + const refreshVal = reducer(_p, _store); |
| 18 | + |
| 19 | + if (isEqual(refreshVal, _p)) return _p; |
| 20 | + return refreshVal; |
15 | 21 | } |
16 | 22 |
|
17 | | - // We need to pass an initialArg otherwise the first *out* will be undefined |
18 | | - const [out, _dispatch] = useReducer(reducer, initReducer.current); |
| 23 | + const [out, _dispatch] = useReducer( |
| 24 | + strictEquality ? reducer : reducerWithEqualityCheck, |
| 25 | + reducer(null, store), |
| 26 | + ); |
| 27 | + |
| 28 | + // Watch dependencies, and dispatch if they change |
| 29 | + useMemo(() => { |
| 30 | + _dispatch(store); |
| 31 | + }, deps); // eslint-disable-line react-hooks/exhaustive-deps |
19 | 32 |
|
20 | | - // For any dependencies in the reducer, we make sure to trigger the reducer again |
21 | | - useMemo(() => _dispatch(store), deps); // eslint-disable-line react-hooks/exhaustive-deps |
22 | 33 |
|
23 | 34 | useEffect(() => { |
| 35 | + // Listener that is called when store is updated |
24 | 36 | function listener() { |
25 | | - // When store is updated, we dispatch an update to the reducer |
| 37 | + // We dispatch the store to the reducer |
26 | 38 | _dispatch(store); |
27 | 39 | } |
28 | 40 |
|
29 | | - // Attach reducer's listener to store |
| 41 | + // Attach listener to store |
30 | 42 | const token = store.addListener(listener); |
31 | 43 |
|
32 | | - // Instead of setting initial value as the second parameter on the useReducer, dispatch here: |
33 | | - // This avoids missing an update between useReducer --> render --> useEffect |
| 44 | + // This avoids potentially missing an update between useReducer --> render --> useEffect |
34 | 45 | _dispatch(store); |
35 | 46 |
|
36 | 47 | // On useEffect destruction, remove the listener |
37 | | - return () => token.remove(); |
| 48 | + return token.remove; |
38 | 49 | }, []); // eslint-disable-line react-hooks/exhaustive-deps |
39 | | - // We make sure to pass [] so we're not attaching/detaching on every render |
| 50 | + // We make sure to pass [] so we're not attaching/detaching on every render |
| 51 | + |
40 | 52 | return out; // Reducer value gets returned to useFluxStore |
41 | 53 | } |
42 | 54 |
|
| 55 | + |
43 | 56 | export default useFluxStore; |
0 commit comments