Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix useSelector race condition with memoized selector when dispatching in child components useLayoutEffect as well as cDM/cDU #1536

Merged
merged 2 commits into from
Apr 19, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/hooks/useSelector.js
Original file line number Diff line number Diff line change
@@ -21,16 +21,19 @@ function useSelectorWithStoreAndSubscription(

const latestSubscriptionCallbackError = useRef()
const latestSelector = useRef()
const latestStoreState = useRef()
const latestSelectedState = useRef()

const storeState = store.getState()
let selectedState

try {
if (
selector !== latestSelector.current ||
storeState !== latestStoreState.current ||
latestSubscriptionCallbackError.current
) {
selectedState = selector(store.getState())
selectedState = selector(storeState)
} else {
selectedState = latestSelectedState.current
}
@@ -44,6 +47,7 @@ function useSelectorWithStoreAndSubscription(

useIsomorphicLayoutEffect(() => {
latestSelector.current = selector
latestStoreState.current = storeState
latestSelectedState.current = selectedState
latestSubscriptionCallbackError.current = undefined
})
38 changes: 37 additions & 1 deletion test/hooks/useSelector.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*eslint-disable react/prop-types*/

import React, { useCallback, useReducer } from 'react'
import React, { useCallback, useReducer, useLayoutEffect } from 'react'
import { createStore } from 'redux'
import { renderHook, act } from '@testing-library/react-hooks'
import * as rtl from '@testing-library/react'
@@ -156,6 +156,42 @@ describe('React', () => {
})
})

it('works properly with memoized selector with dispatch in Child useLayoutEffect', () => {
store = createStore(c => c + 1, -1)

const Comp = () => {
const selector = useCallback(c => c, [])
const count = useSelector(selector)
renderedItems.push(count)
return <Child parentCount={count} />
}

const Child = ({ parentCount }) => {
useLayoutEffect(() => {
if (parentCount === 1) {
store.dispatch({ type: '' })
}
}, [parentCount])
return <div>{parentCount}</div>
}

rtl.render(
<ProviderMock store={store}>
<Comp />
</ProviderMock>
)

// The first render doesn't trigger dispatch
expect(renderedItems).toEqual([0])

// This dispatch triggers another dispatch in useLayoutEffect
rtl.act(() => {
store.dispatch({ type: '' })
})

expect(renderedItems).toEqual([0, 1, 2])
})

describe('performance optimizations and bail-outs', () => {
it('defaults to ref-equality to prevent unnecessary updates', () => {
const state = {}