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:
-
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).
- When the component is added to the React application, initialization begins. The initialization process involves setting up the component's default props (
-
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
propsandstate, returning a React element (typically a virtual DOM node). This returned element can represent native DOM elements or a collection of other components. Notably, therendermethod 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.
-
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, therendermethod is skipped, and subsequent update steps are not executed. - render: The
renderfunction 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.
-
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:
jsxclass 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
Counteris first added to the React tree,constructor,render, andcomponentDidMountare called sequentially. - When the user clicks the button, the
incrementmethod updates the component's state viasetState, triggering the update process. - Since the state changes,
shouldComponentUpdate(if defined) andrenderare called. If necessary,getSnapshotBeforeUpdateandcomponentDidUpdateare also invoked. - When the component is about to be removed,
componentWillUnmountis called.