[英]React native / redux - setState cannot update during an existing state transition
[英]In react native class component , cant not use setstate to update state
我正在使用 this.setState 更新组件中的 state ,但这有问题,this.setState 的回调也没有执行。 这是我的代码:
{
this.state.monthData.map((item, index) => (
<TouchableOpacity onPress={() => this.openMessage(item, index)}
style={[styles.calendarItem,
this.state.activeIndex === index ? {
borderWidth: 1,
borderColor: 'red',
} : {}]}
>
<Text style={styles.dayTitle}>{item.cDay}</Text>
<Text style={styles.dayLunar}>{item.Term ?? item.IDayCn}</Text>
<View style={styles.underline}/>
</TouchableOpacity>
))
}
constructor (props) {
super(props)
this.state = {
monthData: [],
offsetMargin: 0,
activeIndex: null,
}
}
openMessage = (item, index) => {
console.log('index', index) // output correctly
this.setState({
activeIndex: index
}, function (){
console.log('set success') // here is not executed
})
尝试使用这种方式
openMessage = (item, index) => {
console.log('index', index) // output correctly
this.setState({
activeIndex: index
}, () => {
console.log('set success') // here is not executed
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.