Last active
January 16, 2017 10:52
-
-
Save okonet/2a76683bc908f986537d1c7598be2df6 to your computer and use it in GitHub Desktop.
React at 60fps Example 1
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 ScrollPane extends React.Component { | |
componentDidUpdate() { | |
// Each time we get new props we set the | |
// new scrollTop position on the DOM element | |
this.el.scrollTop = this.props.scrollTop | |
} | |
render() { | |
<div ref={(el) => {this.el = el}}> | |
} | |
} | |
class ScrollContainer extends React.Component { | |
constructor() { | |
super() | |
this.leftPane = null | |
this.rightPane = null | |
this.state = { | |
leftPaneScrollTop: 0, | |
rightPaneScrollTop: 0 | |
} | |
} | |
handleLeftScroll = (evt) => { | |
// Calculate new scrollTop positions | |
// for left and right panes based on | |
// DOM nodes and evt.target.scrollTop | |
const leftPaneScrollTop = … | |
const rightPaneScrollTop = … | |
// Don't do this since this will re-render everything | |
// on each `scroll` event! | |
this.setState({ | |
leftPaneScrollTop, | |
rightPaneScrollTop | |
}) | |
} | |
render() { | |
return ( | |
<div> | |
<ScrollPane | |
ref={(el) => {this.leftPane = el}} | |
onScroll={this.handleScroll} | |
scrollTop={this.state.leftPaneScrollTop} | |
> | |
<ExpensiveComponent /> | |
</ScrollPane> | |
<ScrollPane | |
ref={(el) => {this.rightPane = el}} | |
onScroll={this.handleScroll} | |
scrollTop={this.state.rightPaneScrollTop} | |
> | |
<ExpensiveComponent /> | |
</ScrollPane> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment