Skip to content

Commit

Permalink
Don't call setState in Provider if unmounting
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Aug 14, 2018
1 parent 0374936 commit a64103c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/components/Provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,25 @@ export function createProvider(storeKey = 'store') {
this.subscribe();
}

componentWillUnmount() {
if(this.unsubscribe) {
this.unsubscribe()
this._isMounted = false;
}
}

subscribe() {
const {store} = this.props;

this._isMounted = true;

this.unsubscribe = store.subscribe( () => {
const newStoreState = store.getState();

if(!this._isMounted) {
return;
}

this.setState(providerState => {
// If the value is the same, skip the unnecessary state update.
if(providerState.storeState === newStoreState) {
Expand Down

0 comments on commit a64103c

Please sign in to comment.