Skip to content

Commit 217e98a

Browse files
appdentimdorr
authored andcommitted
Fix error from clear() called during notify() (#517)
The `current` variable would be set to `null` when `clear()` was called, which would cause an exception if there was more than one listener. The new test would fail before this change.
1 parent 0050678 commit 217e98a

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/utils/Subscription.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ function createListenerCollection() {
1717
},
1818

1919
notify() {
20-
current = next
21-
for (let i = 0; i < current.length; i++) {
22-
current[i]()
20+
const listeners = current = next
21+
for (let i = 0; i < listeners.length; i++) {
22+
listeners[i]()
2323
}
2424
},
2525

test/components/connect.spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,52 @@ describe('React', () => {
910910
expect(mapStateToPropsCalls).toBe(1)
911911
})
912912

913+
it('should not attempt to notify unmounted child of state change', () => {
914+
const store = createStore(stringBuilder)
915+
916+
@connect((state) => ({ hide: state === 'AB' }))
917+
class App extends Component {
918+
render() {
919+
return this.props.hide ? null : <Container />
920+
}
921+
}
922+
923+
@connect(() => ({}))
924+
class Container extends Component {
925+
render() {
926+
return (
927+
<Child />
928+
)
929+
}
930+
}
931+
932+
@connect((state) => ({ state }))
933+
class Child extends Component {
934+
componentWillReceiveProps(nextProps) {
935+
if (nextProps.state === 'A') {
936+
store.dispatch({ type: 'APPEND', body: 'B' });
937+
}
938+
}
939+
render() {
940+
return null;
941+
}
942+
}
943+
944+
const div = document.createElement('div')
945+
ReactDOM.render(
946+
<ProviderMock store={store}>
947+
<App />
948+
</ProviderMock>,
949+
div
950+
)
951+
952+
try {
953+
store.dispatch({ type: 'APPEND', body: 'A' })
954+
} finally {
955+
ReactDOM.unmountComponentAtNode(div)
956+
}
957+
})
958+
913959
it('should not attempt to set state after unmounting nested components', () => {
914960
const store = createStore(() => ({}))
915961
let mapStateToPropsCalls = 0

0 commit comments

Comments
 (0)