I am using this.setState to update state in my component, but something wrong with this, the callback of this.setState is also not executed. here is my code:
{
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
})
Try using this way
openMessage = (item, index) => {
console.log('index', index) // output correctly
this.setState({
activeIndex: index
}, () => {
console.log('set success') // here is not executed
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.