Skip to content

Commit b3c8b15

Browse files
committed
[v5]: refactor useMemoSelector
1 parent 43986a3 commit b3c8b15

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

src/react.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ const useMemoSelector = <TState, StateSlice>(
2525
selector: (state: TState) => StateSlice,
2626
) =>
2727
useMemo(() => {
28-
let lastSlice: StateSlice
29-
let lastState: TState
28+
let prev: readonly [TState, StateSlice] | undefined
3029
return () => {
3130
const state = getState()
32-
if (!Object.is(lastState, state)) {
33-
lastSlice = selector(state)
34-
lastState = state
31+
if (!prev || !Object.is(prev[0], state)) {
32+
prev = [state, selector(state)]
3533
}
36-
return lastSlice
34+
return prev[1]
3735
}
3836
}, [getState, selector])
3937

src/react/shallow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// import { useDebugValue } from 'react'
1+
// import { useRef } from 'react'
22
// That doesnt work in ESM, because React libs are CJS only.
33
// The following is a workaround until ESM is supported.
44
// eslint-disable-next-line import/extensions

0 commit comments

Comments
 (0)