Skip to content

Commit

Permalink
Merge pull request #283 from pasyukevich/bugfix/state-missing
Browse files Browse the repository at this point in the history
fix: state management flow to avoid data loss
  • Loading branch information
Julesssss authored Aug 30, 2023
2 parents 06f2bd1 + 8af5aaf commit f3141b9
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/withOnyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ export default function (mapOnyxToState) {
return;
}

const stateUpdate = {...this.tempState, loading: false};
// Leave untouched previous state to avoid data loss during pre-load updates.
// This handles case when setState was called before the setWithOnyxState.
// For example, when an Onyx property was updated by keyChanged before the call of the setWithOnyxState.
this.setState((prevState) => {
const remainingTempState = _.omit(this.tempState, _.keys(prevState));

// The state is set here manually, instead of using setState because setState is an async operation, meaning it might execute on the next tick,
// or at a later point in the microtask queue. That can lead to a race condition where
// setWithOnyxState is called before the state is actually set. This results in unreliable behavior when checking the loading state and has been mainly observed on fabric.
this.state = stateUpdate;
return ({...remainingTempState, loading: false});
});

this.setState(stateUpdate); // Trigger a render
delete this.tempState;
}

Expand Down

0 comments on commit f3141b9

Please sign in to comment.