File tree 1 file changed +9
-7
lines changed
1 file changed +9
-7
lines changed Original file line number Diff line number Diff line change @@ -6,15 +6,17 @@ import { shallow } from '../vanilla/shallow.ts'
6
6
7
7
const { useRef } = ReactExports
8
8
9
- export function useShallow < S , U > ( selector : ( state : S ) => U ) : ( state : S ) => U {
10
- const prev = useRef < U > ( )
9
+ const sliceCache = new WeakMap < object , unknown > ( )
11
10
11
+ export function useShallow < S , U > ( selector : ( state : S ) => U ) : ( state : S ) => U {
12
+ const key = useRef ( { } ) . current
12
13
return ( state ) => {
14
+ const prev = sliceCache . get ( key ) as U | undefined
13
15
const next = selector ( state )
14
- return shallow ( prev . current , next )
15
- ? ( prev . current as U )
16
- : // It might not work with React Compiler
17
- // eslint-disable- next-line react-compiler/react-compiler
18
- ( prev . current = next )
16
+ if ( shallow ( prev , next ) ) {
17
+ return prev as U
18
+ }
19
+ sliceCache . set ( key , next )
20
+ return next
19
21
}
20
22
}
You can’t perform that action at this time.
0 commit comments