简体   繁体   中英

How React Virtual DOM works when I set both the changed states and the unchanged states?

How React Virtual DOM diff between the old in-memory DOMs and the new in-memory DOMs? Specifically, is there any difference between that I just set the changed states and that I set both the changed states and the unchanged states? Is there any difference between set Object and Number?

For example: The current state is {name: 'Eric', id: '1234567890', others: {other1: 1, other2: 2}} . Is the diff result this.setState({name: 'Tiger'}) the same with the diff result this.setState({name: 'Tiger', id: '1234567890', others: {other1: 1, other2: 2}}) ?

By default React triggers a re-render on all calls to setState whether the data has changed or not (see setState docs ), so I would expect it to be identical in the cases you've described.

You can override how it behaves using shouldComponentUpdate if you need specific behaviour.

What this means for the actual content of the internal diff it uses is another story - I would expect them to be identical but it feels like a bad idea to rely on specifics of any of this behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM