RN组件在状态更改时会如何重新渲染?

huangapple go评论60阅读模式
英文:

How exactly is RN component rerendered when state changes?

问题

如果 b 被更改,那么 componentB 会重新渲染,但是 ComponentA 会不会重新渲染呢?或者只有当 a 发生变化时 ComponentA 才会重新渲染?

英文:

Let's say I have a global state defined in Redux which consists of an object as:

const data = 
  { 
    a: { value: 1, id: 1 }, 
    b: { value: 2, id: 2 } 
  };

ComponentA defined in one file uses a, componentB defined in another file uses b. If b is changed, componentB is rerendered, but is ComponentA also rerendered? Or ComponentA will only rerender if a changes?

答案1

得分: 2

每个注册监听器以侦听此变量的组件在此变量更改时都会重新渲染

例如

redux

const initiateState = 
  { 
    a: { value: 1, id: 1 }, 
    b: { value: 2, id: 2 } 
  };

componentA

 const valueA = useSelector((state) => state.reducer.a)

当a变化时,componentA会重新渲染,b变化时则不会有任何操作。

英文:

every component that register listener to variable this will re-render when this variable change

for example

redux

const initiateState = 
  { 
    a: { value: 1, id: 1 }, 
    b: { value: 2, id: 2 } 
  };

componentA

 const valueA = useSelector((state) => state.reducer.a)

when a changed, componentA got re-rendered, b change -> do nothing

huangapple
  • 本文由 发表于 2023年7月10日 16:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定