tab切換的時(shí)候更新當(dāng)前選擇的tab,debugger發(fā)現(xiàn)state并沒有更新,請問具體原因是什么,萬分感謝!constructor(props) {
super(props)
this.state = {
selected: 'blacklist'
}
}tabChange(newSelected) { // newSelected = "score"
const {selected} = this.state; // selected = "blacklist"
debugger
if (selected !== newSelected) { // selected = "blacklist", newSelected = "score"
this.setState({
selected: newSelected,
})
const {tabChange} = this.props
tabChange(newSelected)
}
debugger
const state = this.state //state = {selected: "blacklist",}
console.log(state)
}
1 回答

MYYA
TA貢獻(xiàn)1868條經(jīng)驗(yàn) 獲得超4個(gè)贊
setState 是異步的. 你需要在回調(diào)里回去獲取更新后的 state.
this.setState({ selected: newSelected, }, function() { console.log(this.state) });
添加回答
舉報(bào)
0/150
提交
取消