Skip to content

Commit

Permalink
fix timing issue with setting up store subscription inside a connecte…
Browse files Browse the repository at this point in the history
…d component; see issue 1226 (reduxjs#1235)
  • Loading branch information
MrWolfZ authored and timdorr committed Apr 12, 2019
1 parent c033114 commit da4e446
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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

0 comments on commit da4e446

Please sign in to comment.