react总结

1:<JDText style={[styles.tips,this.state.bgColor?{backgroundColor:'green'}:{backgroundColor:'yellow'}]}>{this.state.contentSecTip}</JDText>

动态改变样式,this.state.bgColor在前,样式在后

2:

<JDText style={tips}>{this.state.contentSecTip}</JDText>

tips为上面定义的变量样式

 3:react setstate 即时生效

setState延迟更新的原因在于—state是异步执行的,只有render发生变化的时候才触发this.setState()。

想要在不更新render的情况下立即执行(同步执行),需要在setState后加上一个function函数。 
当然,render更新了之后对state的值没有任何影响。 
现在只是说,在没有更新render的情况下,我们加上了function也能获取到state的值


(二)具体解决方案:

// 1.设置代码
this.setState({
   myState: 'doubi'
   }, function() {
     // stateFunction是需要立即用到
     this.stateFunction()
   })
// 2.在函数中直接调用
stateFunction() {
   console.log('doubi', this.state.myState)
}

转载于:https://www.cnblogs.com/xiaozhumaopao/p/7612246.html

原文地址:https://www.cnblogs.com/twodog/p/12139287.html