Created
August 12, 2016 22:20
-
-
Save ryanflorence/b775597c6c7a83928660a0addcacb2ce to your computer and use it in GitHub Desktop.
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
import React, { Component } from 'react' | |
class ScrollPosition extends Component { | |
state = { | |
top: 0, | |
left: 0 | |
} | |
render() { | |
const child = this.props.children(this.state) | |
return child ? React.cloneElement(child, { | |
ref: (component) => { | |
// seriously, what the crap is going on here ... | |
this.node = component && component._reactInternalInstance._renderedComponent._hostNode | |
}, | |
onScroll: (event) => { | |
this.setState({ | |
top: this.node.scrollTop, | |
left: this.node.scrollLeft | |
}) | |
if (child.props.onScroll) | |
child.props.onScroll(event) | |
} | |
}) : null | |
} | |
} | |
export default ScrollPosition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment