Skip to content

Instantly share code, notes, and snippets.

@doug65536
Last active November 15, 2021 02:20
Show Gist options
  • Save doug65536/6b14b729174e80067373c4e08d8e9342 to your computer and use it in GitHub Desktop.
Save doug65536/6b14b729174e80067373c4e08d8e9342 to your computer and use it in GitHub Desktop.
setStatePromise
export function setStatePromise<S, C extends Component>(
component: C, state: Partial<S>): Promise<void> {
return new Promise((resolve, _) => {
component.setState(state, () => {
resolve();
});
});
}
@doug65536
Copy link
Author

doug65536 commented Nov 15, 2021

You would call it like this:

return setStatePromise(this, {
    somethingInState: 'You thought I was going to say Hello World'
}).then(() => {
 /* state has actually been updated, do something that needs to use new state */
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment