react-diagram
This diagram illustrates how React schedules: mounting, rendering, reconciliation, event handling and effects.
npm install
npm run dev
Code is in App.tsx.
Overview
- Green means Count component and hook instances
- Blue means DOM
- Grey is everything else
Initial rendering, mounting
- Parent renders
- Component instance created
- State hook instance created with initial value 0
- Effect hook instance created
- React elements for component created
- DOM elements created to show "0" as black (default color)
- React runs the effect hook initially
- Effect hook call setState to set counter to 0 - same as it already is
User click, state change
- User clicks on botton in browser window
- DOM fire a click event
- React wraps DOM event in a React event
- React dispatches React event to clickHandler function
- clickHandler function call setState to increment counter to 1
- React triggers a re-rendering of the component
- React elements for component re-rendered
- React reconciles DOM elements with the re-rendered React elements to show "1"
Prop change, effect
- Parent component re-renders the component with color prop updated to "red"
- React elements for component re-rendered
- React reconciles DOM elements with the re-rendered React elements to style the span as "red"
- React runs the effect hook because the color dependency has changed
- Effect hook call setState to reset counter to 0
- React triggers a re-rendering of the component
- React elements for component re-rendered
- React reconciles DOM elements with the re-rendered React elements to show "0"