简体   繁体   中英

React adding to array performance with useState vs push

When I add elements to an array and want to lead to a render of components using that array, I have to use the useState hook and the spread operator, if I'm not mistaken:

setTestArray(array => [...array, data]);

When I want to add an element in normal JavaScript, I just use push. Isn't it much faster to just push an element to an array? Isn't there a way to avoid this problem and eg push an element and then "say to React" that you want the components using that array to rerender? Are there any other ways to add elements to an array/set that have a better performance?

Just getting started with react, so maybe I have overlooked something here:/

React states are immutable, so if you want to change state you have to set the state again. I'm sure under the hood, react does some smart diffing that is able to re-render things smartly.

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