I need to get a width of html element, using React JS. When I do console.log(this.widthPromoLine)
in componentDidMount()
, it works, but when I do this.setState({moveContent: this.widthPromoLine})
, it doesn't.
import React from 'react'
import './index.css'
class Promo extends React.Component {
constructor(props){
super(props)
this.state = {
moveContent: 0
}
}
componentDidMount(){
this.setState({moveContent: this.widthPromoLine})
}
render(){
return <div
className="promo-content"
ref={promoLine => this.widthPromoLine = promoLine.clientWidth}>
</div>
}
}
.promo-content {
width: 1870px;
}