Skip to content

Instantly share code, notes, and snippets.

@oriSomething
Last active April 30, 2017 20:14
Show Gist options
  • Save oriSomething/f013a72b5486104f7d109931a964428b to your computer and use it in GitHub Desktop.
Save oriSomething/f013a72b5486104f7d109931a964428b to your computer and use it in GitHub Desktop.
Make a React Component that render delayed after requestAnimationFrame event
import React, { Component as ReactComponent} from "react";
export function makeRafDelayed(Component) {
return class RafDelayedComponent extends ReactComponent {
state = {
isFirstRender: true,
};
componentDidMount() {
this.setState({
isFirstRender: false,
});
}
shouldComponentUpdate() {
if (this.rafId == null) {
this.rafId = requestAnimationFrame(() => {
this.forceUpdate(() => {
this.rafId = null;
});
});
}
return false;
}
render() {
if (this.state.isFirstRender) {
return null;
} else {
return <Component {...this.props} />;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment