gpt4 book ai didi

reactjs - 如何获取 react 元素的宽度

转载 作者:行者123 更新时间:2023-12-03 12:55:05 25 4
gpt4 key购买 nike

我正在尝试创建一个范围输入,在 slider 拇指上方显示工具提示。

我在网上浏览了一些普通的 JS 示例,似乎我需要元素的宽度才能完成它。

所以我只是想知道如何获取元素宽度?

几乎相当于 JQuery 方法$(element).width()

最佳答案

    class MyComponent extends Component {
constructor(props){
super(props)
this.myInput = React.createRef()
}

componentDidMount () {
console.log(this.myInput.current.offsetWidth)
}

render () {
return (
// new way - as of React@16.3
<div ref={this.myInput}>some elem</div>
// legacy way
// <div ref={(ref) => this.myInput = ref}>some elem</div>
)
}
}

关于reactjs - 如何获取 react 元素的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43817118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com