What happens after react calls setstate

WBOY
Release: 2022-04-27 15:00:04
Original
3405 people have browsed it

What happens after react calls setstate: 1. Merge the incoming parameter object with the current state of the component to trigger the reconciliation process; 2. Build the React element tree based on the new state and re-render the entire UI interface; 3. , after getting the element tree, React will calculate the node difference between the new tree and the old tree, and then minimize and re-render.

What happens after react calls setstate

The operating environment of this tutorial: Windows 10 system, react17.0.1 version, Dell G3 computer.

What happened after react called setstate

React designed the setState method to re-render the page

Two ways to write setState() to update the state

setState(updater, [callback]), updater is a function that returns a stateChange object: (state, props) => stateChange The state and props received by stateChange are guaranteed to be the latest

setState(stateChange , [callback]), stateChange is an object, callback is an optional callback function, which is executed after the status is updated and the interface is updated.

Summary: The object method is the abbreviation of the function method if the new state does not depend on the original State ===> Use the object method if the new state depends on the original state ===> Use the function method if you need to get the latest state data after setState(), read it in the second callback function

What happens after calling setState?

After calling the setState function in the code, React will merge the passed parameter object with the current state of the component, and then trigger the so-called reconciliation process (Reconciliation).

After the reconciliation process, React will build the React element tree based on the new state in a relatively efficient way and proceed to re-render the entire UI interface.

After React obtains the element tree, React will automatically calculate the node difference between the new tree and the old tree, and then minimize and re-render the interface based on the difference.

In the difference calculation algorithm, React can relatively accurately know which positions have changed and how they should be changed, which ensures on-demand updates instead of full re-rendering.

Recommended learning: "react video tutorial"

The above is the detailed content of What happens after react calls setstate. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template