我正在尝试创建一个范围输入,在 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/