From b2c49bdada975a4f66883edfcfccf580f4203a45 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Wed, 2 Oct 2019 22:01:17 -0400 Subject: [PATCH] fix: race condition on react HOC --- src/react/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/react/index.js b/src/react/index.js index 77607d1..2057cbe 100644 --- a/src/react/index.js +++ b/src/react/index.js @@ -21,16 +21,16 @@ export function observe(WrappedComponent) { const subscription = this.props[prop].subscribe( value => { - this.setState({ + this.setState(state => ({ observedValues: { - ...this.state.observedValues, + ...state.observedValues, [prop]: value } - }); + })); }, err => { // TODO: pass the error to the wrapped component - console.err(err); + console.error(err); } ); @@ -41,11 +41,11 @@ export function observe(WrappedComponent) { } }); } - + componentDidMount() { Object.keys(this.props).forEach(this.subscribeToProp); } - + componentWillUnmount() { this.state.subscriptions.forEach(subscription => { subscription.unsubscribe(); @@ -64,7 +64,7 @@ export function observe(WrappedComponent) { // TODO: check if prevProps and currProps are different, and unsubscribe from prevProp } - + render() { const props = Object.keys(this.props).reduce((accum, curr) => { if(!isObservable(this.props[curr])){