reboottime / React-Develpment

Some notes, thought, articles or source code readings aggregated here about React Web development.
0 stars 0 forks source link

[Advanced React] React Reconciliation #78

Open reboottime opened 3 months ago

reboottime commented 3 months ago

About

This article is a note of Mastering React Reconciliation - Advanced React course


React Reconciliation: how React decides which component should be rendered/ removed

reboottime commented 3 months ago

Practices

reboottime commented 3 months ago

React Conciliation Process

React updates the part that needs update, instead of removing and adding a new one, by Virtual DOM.

The virtual DOM is a giant object that supposed to render all their props and children. Which are also the same shape that are supposed to be rendered inside.

For example

image image
reboottime commented 3 months ago

Bad Practices

Example 2, The key application

image

Though there is a re-rendering between the isCompany value changes, the internal state of Input preserves

image

Example 3, how React reads array

image image

If there is key defined on element, the key order takes priority.

reboottime commented 3 months ago

Virtual DOM Example

{
  "type": "FormGroup",
  "props": {
    "id": "",
    "controlId": "",
    "children": {
      "type": "div",
      "props": {
        "className": ""
      },
      "children": [
        {
          "type": "FormLabel",
          "props": {
            "className": ""
          },
          "children": "Label Text"
        },
        {
          "type": "FormControl",
          "props": {
            "type": "text",
            "placeholder": "Hello",
            "className": ""
          },
          "children": []
        },
        {
          "type": "FormText",
          "props": {
            "className": ""
          },
          "children": "Help text"
        }
      ]
    }
  }
}