Skip to content

Instantly share code, notes, and snippets.

@peggyrayzis
Last active March 1, 2018 16:42
Show Gist options
  • Save peggyrayzis/2f78f91621f5b08e7eb377a1353e8f59 to your computer and use it in GitHub Desktop.
Save peggyrayzis/2f78f91621f5b08e7eb377a1353e8f59 to your computer and use it in GitHub Desktop.
Async React Low Priority
import React from "react";
import ReactDOM from "react-dom";
export class AsyncValue extends React.Component {
state = { asyncValue: this.props.defaultValue };
deferSetState(state) {
ReactDOM.unstable_deferredUpdates(() => {
this.setState(state);
});
}
componentDidMount() {
this.deferSetState((state, props) => ({ asyncValue: props.value }));
}
componentDidUpdate() {
if (this.props.value !== this.state.asyncValue) {
this.deferSetState((state, props) => ({ asyncValue: props.value }));
}
}
render() {
return this.props.children(this.state.asyncValue);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment