Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix timing issue with setting up store subscription inside a connected component #1235

Merged
merged 1 commit into from Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/connectAdvanced.js
Expand Up @@ -291,7 +291,7 @@ export default function connectAdvanced(
})

// Our re-subscribe logic only runs when the store/subscription setup changes
useEffect(() => {
useIsomorphicLayoutEffect(() => {
// If we're not subscribed to the store, nothing to do here
if (!shouldHandleStateChanges) return

Expand Down
32 changes: 32 additions & 0 deletions test/components/connect.spec.js
Expand Up @@ -1926,6 +1926,38 @@ describe('React', () => {

expect(mapStateToProps).toHaveBeenCalledTimes(2)
})

it('should not notify nested components after they are unmounted', () => {
@connect(state => ({ count: state }))
class Parent extends Component {
render() {
return this.props.count === 1 ? <Child /> : null
}
}

const mapStateToProps = jest.fn(state => ({ count: state }))
@connect(mapStateToProps)
class Child extends Component {
render() {
return <div>{this.props.count}</div>
}
}

const store = createStore((state = 0, action) =>
action.type === 'INC' ? state + 1 : state
)
rtl.render(
<ProviderMock store={store}>
<Parent />
</ProviderMock>
)

expect(mapStateToProps).toHaveBeenCalledTimes(0)
store.dispatch({ type: 'INC' })
expect(mapStateToProps).toHaveBeenCalledTimes(1)
store.dispatch({ type: 'INC' })
expect(mapStateToProps).toHaveBeenCalledTimes(1)
})
})

describe('Custom context and store-as-prop', () => {
Expand Down