Skip to content

Instantly share code, notes, and snippets.

@sieverk
Created April 6, 2017 20:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save sieverk/b8815bf0b64e1fc0ef0aac298b7298f4 to your computer and use it in GitHub Desktop.
Save sieverk/b8815bf0b64e1fc0ef0aac298b7298f4 to your computer and use it in GitHub Desktop.
import hoistStatics from 'hoist-non-react-statics';
import React from 'react';
/**
* Allows two animation frames to complete to allow other components to update
* and re-render before mounting and rendering an expensive `WrappedComponent`.
*/
export default function deferComponentRender(WrappedComponent) {
class DeferredRenderWrapper extends React.Component {
constructor(props, context) {
super(props, context);
this.state = { shouldRender: true };
}
componentDidMount() {
window.requestAnimationFrame(() => {
window.requestAnimationFrame(() => this.setState({ shouldRender: true }));
});
}
render() {
return this.state.shouldRender ? <WrappedComponent {...this.props} /> : null;
}
}
return hoistStatics(DeferredRenderWrapper, WrappedComponent);
}
@thomasdavis
Copy link

@sieverk should #12 be shouldRender:false?

@jstejada
Copy link

Cool! I actually had the same question as @thomasdavis! Also, do you mind sharing the reasoning behind deferring using 2 calls to requestAninationFrame? Thanks!

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