Reputation: 192
I've read that componentDidMount runs after render in React components, but I'm still a bit confused. Let's say I'm making a request in componentDidMount to an API to populate my state, but if render runs before componentDidMount, how is my application displaying the information to the user? Is the React component doing something like render() -> componentDidMount() -> render()? And if so, what is the benefit to running render() before componentDidMount()?
Upvotes: 0
Views: 45
Reputation: 4987
A component is rerendered when you update its state or when a prop changes. So if you update its state in componentDidMount(), then the component rerender.
ComponentDidMount exists as a way for you to know that your component is mounted, as its name says. So for example if you need to get the height of something from your jsx, you could do it here, you won't get an error saying your reference is undefined, because your component is mounted and so the node exists.
Upvotes: 1