I've just had a look at this discussion about setState()
inside componentDidMount()
.
You can see that after the
render()
function, thecomponentDidMount()
function will be called by React. When you put asetState()
call incomponentDidMount()
then you are causing the entire component tree be re-rendered not only the current component - not to forget, the current component did just finished with rendering.
And some people suggested to put setState()
call inside componentWillMount()
. In some cases, I want to get the height of a rendered element and store it as state, and the above method wouldn't work. I also had a look at the React official website, and it suggests to do Ajax call inside componentDidMount()
, which again goes against the above idea.
So, am I wrong about putting setState()
inside componentDidMount()
? If yes, what should I apply as an alternative?