Render and Commit
Before components are displayed on the screen, they must be rendered by React
Requesting / serving UI has 3 steps
- Triggering render (delivering order to kitchen)
- Rendering component (preparing order in the kitchen)
- Comitting to the DOM (placing order on the table)
1. Triggering a render Happens for two reasons
- Initial render: when app starts
- Re-render: When state updates
- Updating state automatically queues a render
2. React Renders your components React figures out what to display on the screen by calling your components
On initial render- react calls root component Subsequent render- calls function component whose state update triggered render
- If has children, recursive rendering of component’s children
3. React Commits changes to the DOM After rendering, the actual modifying of the DOM
Initial render- uses appendChild()
DOM API to put all DOM nodes it has on screen
Re-renders: minmial necessary operations (calculated while rendering) to make DOM match the latest rendering output
- Only changes DOM nodes if difference between renders
State as a Snapshot
Think of state as a snapshot
- Updating state doesn’t change the state varibale you have, but triggers a rerender
- Thus, if you change state then immediately log, the state will seem the same
Remember: Setting state triggers a re-render
Rendering takes a snapshot in time, when React re-renders a component
- React calls your function again
- Your function returns a new JSX snapshot
- React then updates the screen to match the snapshot your function returned
State lives in React itself
Setting state only changes it for the next render (thus, doing setNumber(number+1)
10 times is the same as once)
Event handlers created in the past have state values from the render inw hich they were created