如何在react中获取原生html元素或在props中作为子元素传递的元素的边界?
给定下一个组件:
class SomeComp extends Component {
componentDidMount() {
this.props.children.forEach(child => {
// I need the native element to get the offsetTop and the .getBoundingClientRect() func of html
})
}
render() {
<div>
{this.props.children}
</div>
}
}
发布于 2018-06-17 16:33:38
这是我找到的最好的解决方案,它恰好满足了我的需求
只需映射到子元素,并返回带有额外ref的元素的克隆
this.props.children.map((element, index) => (
<element.type {...element.props} ref={r => do what ever with ref} key={index}> {element.props.children} </element.type>
)
https://stackoverflow.com/questions/50858262
复制相似问题