Skip to content

Commit

Permalink
[v5]: refactor useMemoSelector
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jan 20, 2024
1 parent 43986a3 commit b3c8b15
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions src/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ const useMemoSelector = <TState, StateSlice>(
selector: (state: TState) => StateSlice,
) =>
useMemo(() => {
let lastSlice: StateSlice
let lastState: TState
let prev: readonly [TState, StateSlice] | undefined
return () => {
const state = getState()
if (!Object.is(lastState, state)) {
lastSlice = selector(state)
lastState = state
if (!prev || !Object.is(prev[0], state)) {
prev = [state, selector(state)]
}
return lastSlice
return prev[1]
}
}, [getState, selector])

Expand Down
2 changes: 1 addition & 1 deletion src/react/shallow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// import { useDebugValue } from 'react'
// import { useRef } from 'react'
// That doesnt work in ESM, because React libs are CJS only.
// The following is a workaround until ESM is supported.
// eslint-disable-next-line import/extensions
Expand Down

0 comments on commit b3c8b15

Please sign in to comment.