Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React lifecyle: phases

As much as I know, this are the "phases" on a react lifecycle for a component:

Mounting: Mounting is the stage of rendering the JSX returned by the render method itself. Updating: Updating is the stage when the state of a component is updated and the application is repainted. Unmounting: As the name suggests Unmounting is the final step of the component lifecycle where the component is removed from the page.

mounting -> updating -> unmounting

I succesfully used it, but i don't know how to mix those 3 "phases" with : - commit phase - precommit phase - render phase

I found this : http://projects.wojtekmaj.pl/react-lifecycle-methods-diagram/

I'm not sure if "mounting", "updating" and "unmounting" are phases or somethings else. Can it be the case that phases are: commit, precommit and render. And "mouting, updating and unmounting" are events or stages ?

Any clues?

like image 631
Ricardo Daniel Avatar asked Dec 13 '25 08:12

Ricardo Daniel


1 Answers

The "render phase" and "commit phase" are descriptions of the internal process that react goes through to update your page. While it can be useful to understand what's going on in these, the only place you can interact with it is through the various lifecycle hooks, such as componentDidMount, componentDidUpdate, and componentWillUnmount, so those are the things i would recommend you focus your research on.

When it's time to render the page (usually caused by calling this.setState somewhere), react goes through a series of steps in order to update the webpage:

The first group of steps are collectively called the "render phase". In the render phase, react is creating the Virtual DOM. In other words, it's determining what the page is supposed to look like, without actually changing the page. In the simplest form (where we're not skipping any renders with things like react.memo or shouldComponentUpdate), react calls render on the topmost component, and finds out what it returned, and then for each of its children it calls render on those as well until it knows what the whole page should look like.

The second group of steps are called the "commit phase". Now that it knows what the page should look like, it needs to update the actual DOM to match the Virtual DOM. To do this, it compares the current virtual DOM that it got from the render phase with the virtual DOM it got the last time it rendered, and figures out the minimum set of updates in order to make the page look like that.

And now the rendering is done and the page has been updated. During this process it's possible that some components got created for the first time (ie, "mounted"), or they got their props changed (ie, "updated"), or they were removed entirely ("unmounted"). Components that had this happen to them will have their componentDidMount, componentDidUpdate and componentWillUnmount functions called as appropriate.

like image 86
Nicholas Tower Avatar answered Dec 16 '25 01:12

Nicholas Tower