首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >React获取在属性中传递的子元素的本机元素或高度和宽度

React获取在属性中传递的子元素的本机元素或高度和宽度
EN

Stack Overflow用户
提问于 2018-06-14 21:04:15
回答 1查看 3.5K关注 0票数 1

如何在react中获取原生html元素或在props中作为子元素传递的元素的边界?

给定下一个组件:

代码语言:javascript
复制
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>
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-17 16:33:38

这是我找到的最好的解决方案,它恰好满足了我的需求

只需映射到子元素,并返回带有额外ref的元素的克隆

代码语言:javascript
复制
this.props.children.map((element, index) => (
    <element.type {...element.props} ref={r => do what ever with ref} key={index}> {element.props.children} </element.type>
)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50858262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档