Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Created August 12, 2016 22:20
Show Gist options
  • Save ryanflorence/b775597c6c7a83928660a0addcacb2ce to your computer and use it in GitHub Desktop.
Save ryanflorence/b775597c6c7a83928660a0addcacb2ce to your computer and use it in GitHub Desktop.
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