Skip to content

Commit 07365ee

Browse files
authoredMar 25, 2021
Don't re-run the selector after update (#1701)
1 parent 010c3ee commit 07365ee

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed
 

‎src/hooks/useSelector.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ function useSelectorWithStoreAndSubscription(
6464
useIsomorphicLayoutEffect(() => {
6565
function checkForUpdates() {
6666
try {
67-
const newSelectedState = latestSelector.current(store.getState())
67+
const newStoreState = store.getState()
68+
const newSelectedState = latestSelector.current(newStoreState)
6869

6970
if (equalityFn(newSelectedState, latestSelectedState.current)) {
7071
return
7172
}
7273

7374
latestSelectedState.current = newSelectedState
75+
latestStoreState.current = newStoreState
7476
} catch (err) {
7577
// we ignore all errors here, since when the component
7678
// is re-rendered, the selectors are called again, and

‎test/hooks/useSelector.spec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,21 @@ describe('React', () => {
3838
})
3939

4040
it('selects the state and renders the component when the store updates', () => {
41-
const { result } = renderHook(() => useSelector((s) => s.count), {
41+
const selector = jest.fn((s) => s.count)
42+
43+
const { result } = renderHook(() => useSelector(selector), {
4244
wrapper: (props) => <ProviderMock {...props} store={store} />,
4345
})
4446

4547
expect(result.current).toEqual(0)
48+
expect(selector).toHaveBeenCalledTimes(2)
4649

4750
act(() => {
4851
store.dispatch({ type: '' })
4952
})
5053

5154
expect(result.current).toEqual(1)
55+
expect(selector).toHaveBeenCalledTimes(3)
5256
})
5357
})
5458

0 commit comments

Comments
 (0)
Please sign in to comment.