Skip to content

Instantly share code, notes, and snippets.

@franzejr
Created April 27, 2016 02:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franzejr/69abc27e3c1d53fae0f1990e8d3e0502 to your computer and use it in GitHub Desktop.
Save franzejr/69abc27e3c1d53fae0f1990e8d3e0502 to your computer and use it in GitHub Desktop.
withScroll Higher Order Component
var getScroll = function() {
return (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop
}
var withScroll = function(Component) {
return React.createClass({
getInitialState: function() {
return {
scroll: getScroll()
}
},
componentDidMount: function () {
var handleScroll =() => {
var scroll = getScroll()
this.setState({ scroll: scroll })
};
document.addEventListener('scroll', handleScroll);
this.unsubscribe = function() {
document.removeEventLister('scroll', handleScroll);
};
},
componentWillUnmount: function() {
this.unsubscribe();
},
render: function() {
return <Component {...this.props} {...this.state} />
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment