File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change 1
- import { useMemo } from 'react' ;
1
+ import { useRef } from 'react' ;
2
2
import { observable } from '@nx-js/observer-util' ;
3
3
4
4
import {
@@ -12,7 +12,13 @@ export function store(obj) {
12
12
// if it is a local store in a function component
13
13
// create a memoized store at the first call instead
14
14
if ( isInsideFunctionComponent ) {
15
- return useMemo ( ( ) => observable ( obj ) , [ ] ) ;
15
+ // we have to use useRef instead of useMemo for local store persistence
16
+ // useMemo does not have the same guarantees about persistence as refs
17
+ // see this docs for more explanation: https://reactjs.org/docs/hooks-reference.html#usememo
18
+ const ref = useRef ( obj ) ;
19
+ // observable wrapping is idempotent
20
+ // wrapping the same object multiple times is the same as wrapping it once
21
+ return observable ( ref . current ) ;
16
22
}
17
23
if ( isInsideFunctionComponentWithoutHooks ) {
18
24
throw new Error (
You can’t perform that action at this time.
0 commit comments