简体   繁体   English

功能组件中每个独立 state 的更改是否会触发 React 中组件的渲染?

[英]Does change in each independent state in functional component triggers a render for the component in React?

When we have multiple independent states in a component then if I change any one of them, will it trigger a re-render of the entire component?当我们在一个组件中有多个独立状态时,如果我更改其中任何一个,是否会触发整个组件的重新渲染?

Initialise const [email, setEmail] = useState('') const [val, setVal] = useState(true)初始化const [email, setEmail] = useState('') const [val, setVal] = useState(true)

Update setVal(false)更新setVal(false)

Will this render the whole component again?这会再次渲染整个组件吗?

Yes the Component will be rendered again.是的,组件将再次呈现。 Let me add: Although if you have two states changing (almost) simultanously the state update will be batched.让我补充一下:虽然如果您有两个状态(几乎)同时发生变化,则 state 更新将被批处理。 In React 18 React batches state updates in React events handlers, promises, setTimeout and native event handlers resulting in only one rerender.在 React 18 中,React 批处理 state 更新 React 事件处理程序、promise、setTimeout 和本机事件处理程序,导致仅重新渲染一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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