tab切換的時候更新當(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)
}
3 回答

www說
TA貢獻(xiàn)1775條經(jīng)驗 獲得超8個贊
setState 是異步的. 你需要在回調(diào)里回去獲取更新后的 state.
this.setState({ selected: newSelected, }, function() { console.log(this.state) });

慕的地8271018
TA貢獻(xiàn)1796條經(jīng)驗 獲得超4個贊
react在生命周期和event handler里的setState會被合并(異步)處理。(17版本以后全都會變異步)
添加回答
舉報
0/150
提交
取消