Created
September 12, 2018 15:39
-
-
Save xnimorz/7b9bebec332864615dab42e4be271fb5 to your computer and use it in GitHub Desktop.
deffered render
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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