There are a number of templates to achieve this. This pattern is what I came up with to lift you up.
First, create a component class that has a large element for the scroll effect. When you drag the scroll bar, this component calls the handleScroll React property to notify its parent component with a value of scrollTop .
var Elem = React.createClass({ render() { return ( <div ref="elem" onScroll={ this.onScroll } style={{ width: "100px", height: "100px", overflow: "scroll" }}> <div style={{ width: "100%", height: "200%" }}>Hello!</div> </div> ); }, componentDidUpdate() { this.refs.elem.scrollTop = this.props.scrollTop; }, onScroll() { this.props.handleScroll( this.refs.elem.scrollTop ); } });
The parent component, aka wrapper, keeps the top scroll value in its state. Its handleScroll is passed to the child components as a callback. Any scrolling through the children calls a callback, sets the state, leads to redrawing and updates the child component.
var Wrapper = React.createClass({ getInitialState() { return { scrollTop: 0 } }, render() { return ( <div> <Elem scrollTop={ this.state.scrollTop } handleScroll={ this.handleScroll } /> <Elem scrollTop={ this.state.scrollTop } handleScroll={ this.handleScroll } /> </div> ); }, handleScroll( scrollTop ) { this.setState({ scrollTop }); } });
And draw the shell, assuming the existing <div id="container"></div> .
ReactDOM.render( <Wrapper />, document.getElementById('container') );
source share