DEV Community

Discussion on: Accessing React State right after setting it

Collapse
 
revskill10 profile image
Truong Hoang Dung • Edited

You can read it in the reactjs documentation, it says:

You may call setState() immediately in componentDidUpdate() but note that it must be wrapped in a condition like in the example above, or you’ll cause an infinite loop.

The example is

componentDidUpdate(prevProps) { // Typical usage (don't forget to compare props): if (this.props.userID !== prevProps.userID) { this.fetchData(this.props.userID); } } 

In my case, i update and re-render component whenever i receive new data from websocket.

Thread Thread
 
dance2die profile image
Sung M. Kim

it must be wrapped in a condition

Ah without it, a component definitely state to update infinitely.

Thanks Truong.