diff --git a/src/hooks/useSelector.js b/src/hooks/useSelector.js index 39066e776..87b61f244 100644 --- a/src/hooks/useSelector.js +++ b/src/hooks/useSelector.js @@ -64,13 +64,15 @@ function useSelectorWithStoreAndSubscription( useIsomorphicLayoutEffect(() => { function checkForUpdates() { try { - const newSelectedState = latestSelector.current(store.getState()) + const newStoreState = store.getState() + const newSelectedState = latestSelector.current(newStoreState) if (equalityFn(newSelectedState, latestSelectedState.current)) { return } latestSelectedState.current = newSelectedState + latestStoreState.current = newStoreState } catch (err) { // we ignore all errors here, since when the component // is re-rendered, the selectors are called again, and diff --git a/test/hooks/useSelector.spec.js b/test/hooks/useSelector.spec.js index 3a0fcfd7d..0af0176c7 100644 --- a/test/hooks/useSelector.spec.js +++ b/test/hooks/useSelector.spec.js @@ -38,17 +38,21 @@ describe('React', () => { }) it('selects the state and renders the component when the store updates', () => { - const { result } = renderHook(() => useSelector((s) => s.count), { + const selector = jest.fn((s) => s.count) + + const { result } = renderHook(() => useSelector(selector), { wrapper: (props) => , }) expect(result.current).toEqual(0) + expect(selector).toHaveBeenCalledTimes(2) act(() => { store.dispatch({ type: '' }) }) expect(result.current).toEqual(1) + expect(selector).toHaveBeenCalledTimes(3) }) })