Skip to content

Instantly share code, notes, and snippets.

@xnimorz
Created September 12, 2018 15:39
Show Gist options
  • Select an option

  • Save xnimorz/7b9bebec332864615dab42e4be271fb5 to your computer and use it in GitHub Desktop.

Select an option

Save xnimorz/7b9bebec332864615dab42e4be271fb5 to your computer and use it in GitHub Desktop.
deffered render
class DeferredRender extends Component {
static defaultProps = {
countByStep: 100,
};
state = { rendered: 0 };
componentDidMount() {
this.nextStep();
}
componentWillReceiveProps(nextProps) {
if (nextProps.children.length !== this.props.children.length) {
this.setState({ rendered: 0 });
}
}
componentDidUpdate() {
this.nextStep();
}
nextStep() {
const { rendered } = this.state;
const { children, countByStep } = this.props;
if (children.length > rendered + countByStep) {
requestAnimationFrame(() => {
this.setState(state => ({
rendered: state.rendered + countByStep,
}));
});
}
}
render() {
const { rendered } = this.state;
const { children, countByStep } = this.props;
if (children < countByStep) {
return <Deferred>{children}</Deferred>;
}
return <Deferred>{children.slice(0, rendered + countByStep)}</Deferred>;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment