Skip to content

Commit

Permalink
fix(withEventrixState): race condition on componentDidMount
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariusz Przodała committed Apr 7, 2021
1 parent ea3bb22 commit 05f1194
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/react/hocs/withEventrixState.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const withEventrixState = (BaseComponent, stateNames, mapStateToProps, Context =
this.getStateNames().forEach((stateName) => {
this.context.eventrix.listen(`setState:${stateName}`, this.listeners[stateName]);
});
this.refreshState();
}
componentWillUnmount() {
this.getStateNames().forEach((stateName) => {
Expand Down Expand Up @@ -47,6 +48,20 @@ const withEventrixState = (BaseComponent, stateNames, mapStateToProps, Context =
}
return this.state;
}
refreshState() {
let shouldRefreshState = false;
const stateToRefresh = {};
this.getStateNames().forEach((stateName) => {
const currentState = this.context.eventrix.getState(stateName);
if (this.state[stateName] !== currentState) {
shouldRefreshState = true;
stateToRefresh[stateName] = currentState;
}
});
if (shouldRefreshState) {
this.setState(stateToRefresh);
}
}
render() {
return <BaseComponent {...this.props} {...this.getStateForProps()} />;
}
Expand Down

0 comments on commit 05f1194

Please sign in to comment.