5月27日 01:12

How Does the Rendering Process of React Components Work?

The rendering process of React components is generally divided into several steps:

  1. Initialization Phase:

    • When the component is added to the React application, initialization begins. The initialization process involves setting up the component's default props (defaultProps) and initial state (state).
  2. Mounting Phase (Mounting):

    • constructor: If the component is a class component, the constructor is called first to initialize state and perform other setup operations.
    • getDerivedStateFromProps (optional): This method is invoked after the component instance is created but before re-rendering. It can be used to update the state based on incoming props.
    • render: This method is the core of component rendering. It analyzes the current component's props and state, returning a React element (typically a virtual DOM node). This returned element can represent native DOM elements or a collection of other components. Notably, the render method is a pure function and must not contain any code that modifies the component's state.
    • componentDidMount: This method is called after the component is mounted. It is the ideal place to perform side effects, such as making network requests or setting timers.
  3. Updating Phase (Updating): When the component's props or state change, it re-renders following this sequence:

    • getDerivedStateFromProps (optional): As mentioned earlier, this method updates the state based on new props when props change.
    • shouldComponentUpdate (optional): This method determines whether the component should update based on its return value. If it returns false, the render method is skipped, and subsequent update steps are not executed.
    • render: The render function is executed again, identical to the initialization phase.
    • getSnapshotBeforeUpdate (optional): This method is called before the DOM updates to capture the state of the DOM prior to the change.
    • componentDidUpdate: This method is called after the component updates. It can be used for operations such as updating the DOM.
  4. Unmounting Phase (Unmounting):

    • componentWillUnmount: This method is called before the component is unmounted to perform necessary cleanup, such as clearing timers or canceling network requests.

During this process, React optimizes the component tree using virtual DOM and Diff algorithm to minimize actual DOM operations, thereby improving performance.

Example: Suppose we have a simple counter component Counter with a button to increment the count, where the count value is stored in the state count. When the user clicks the button, the component's state is updated, triggering the update process:

jsx
class Counter extends React.Component { constructor(props) { super(props); this.state = { count: 0 }; } componentDidMount() { console.log('Counter: componentDidMount'); } componentDidUpdate() { console.log('Counter: componentDidUpdate'); } componentWillUnmount() { console.log('Counter: componentWillUnmount'); } increment = () => { this.setState(state => ({ count: state.count + 1 })); }; render() { return ( <div> <p>Count: {this.state.count}</p> <button onClick={this.increment}>Increment</button> </div> ); } }

In this example:

  • When Counter is first added to the React tree, constructor, render, and componentDidMount are called sequentially.
  • When the user clicks the button, the increment method updates the component's state via setState, triggering the update process.
  • Since the state changes, shouldComponentUpdate (if defined) and render are called. If necessary, getSnapshotBeforeUpdate and componentDidUpdate are also invoked.
  • When the component is about to be removed, componentWillUnmount is called.
标签:React前端