Created
April 27, 2016 02:28
-
-
Save franzejr/69abc27e3c1d53fae0f1990e8d3e0502 to your computer and use it in GitHub Desktop.
withScroll Higher Order Component
This file contains 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
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