Skip to content

Instantly share code, notes, and snippets.

@peggyrayzis
Created March 1, 2018 16:47
Show Gist options
  • Save peggyrayzis/16fbeadcbc7fcedd28b0c29a2d3aa649 to your computer and use it in GitHub Desktop.
Save peggyrayzis/16fbeadcbc7fcedd28b0c29a2d3aa649 to your computer and use it in GitHub Desktop.
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