Reactscrolldecorator

17 May 2017

Update, December 2017

I love JavaScript decorators. One that I copy to every React project is scroll decorator. It is fairly simple way of injecting scroll position to react components. This way you can handle scroll using react lifecycle.

This decorator is not listening to a scroll event, as that is the performance killer (especially when you push it to react lifecycle). Instead, it is using interval to check if scroll position has changed. To keep it performant, I’m using requestAnimationFrame. Interval is only there to throttle animation frame from triggering too often.

Please note that it still may cause performance issues if you apply it to a large number of components. Personally, I never had to apply it to a more than three of four per page.

So here it is:

importReact,{Component}from'react';constwithScroll=ComposedComponent=>classScrollDecoratorextendsComponent{constructor(){super();// Initial scroll positionthis.state={scrollPosition:this.getWindowScrollTop(),};// Bind handlersthis.handleInterval=this.handleInterval.bind(this);this.handleRequestAnimationFrame=this.handleRequestAnimationFrame.bind(this);}componentWillMount(){// 50 times per second, change to your needsconstINTERVAL=20;this.intervalID=setInterval(this.handleInterval,INTERVAL);}componentWillUnmount(){// Remove and reset interval/animationFrameclearInterval(this.intervalID);cancelAnimationFrame(this.requestID);this.requestID=null;this.intervalID=null;}getWindowScrollTop(){// Get scroll position, with IE fallbackreturnwindow.pageYOffset||document.documentElement.scrollTop;}handleInterval(){// Interval is only used to throttle animation framecancelAnimationFrame(this.requestID);this.requestID=requestAnimationFrame(this.handleRequestAnimationFrame);}handleRequestAnimationFrame(){const{scrollPosition}=this.state;constnewScrollPosition=this.getWindowScrollTop();// Update the state only when scroll position is changedif(newScrollPosition!==scrollPosition){this.setState({scrollPosition:newScrollPosition,});}}render(){const{scrollPosition}=this.state;return(<ComposedComponent{...this.props}scrollPosition={scrollPosition}/>
);}};exportdefaultwithScroll;