this.text = 'a' vs this.setState({text:a'}) 有什么不同

  • 本文关键字:this text vs setState react-native
  • 更新时间 :
  • 英文 :


我知道组件在状态更改时会重新渲染。但在这个代码中:

<ColorPicker
onColorSelected={color => this.color = color}
style={{ width: width / 2, height: 200 }}
/>
<View style={{ width: width / 2, height: 200, justifyContent: 'center' }}>
<AnimatedText style={{ fontSize: 25, fontWeight: "bold", color:  this.color }}> asdasdasdasdasd</AnimatedText>
</View>

没有状态改变,但是文本的颜色仍然改变。那么setState是什么意思呢?而我这样做,是不是会让表现更好?

this.setState({text:a'})将调用呈现方法,this.text = 'a'不会。


react建议使用状态道具更新ui。


this.text = 'a'将在分配后工作,但this.setState({text:a'})将在批中工作

最新更新