2 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
你的語法不正確。當(dāng)您調(diào)用時(shí),handleMaxLevel您應(yīng)該向其傳遞事件對(duì)象。并且setState回調(diào)不需要任何參數(shù),它已經(jīng)可以訪問更新的狀態(tài)。所以你的onChange處理程序應(yīng)該是這樣的:
onChange={handleMaxLevel} // If we pass function directly, it will be given the event argument by default
然后在setState回調(diào)中直接引用狀態(tài)值,因?yàn)樵诨卣{(diào)中它們將被更新:
handleMaxLevel = event => {
this.setState({
...this.state,
maxLevel: event.target.value
}, () => {
let filter = this.state.filteredPlayers.filter(players => {
return players.level > this.state.minLevel && players.level < this.state.maxLevel
})
this.setState({
...this.state,
levelFilteredPlayers: filter
})
})
}
編輯:我相信您也需要傳播舊狀態(tài),以防止值丟失。另外,最好在一次setState調(diào)用中完成所有這些操作,而不是使用回調(diào)

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊
這里出了問題是你的函數(shù)需要一個(gè)參數(shù)
const handleMaxLevel = event => {
// ...
}
但是,您的調(diào)用正在發(fā)送兩個(gè)參數(shù)
onChange={() => this.handleMaxLevel(this.state.minLevel, this.state.maxLevel)}
如果您想更改函數(shù)以接受事件和狀態(tài)變量變量:
// Change invocation like so:
onChange={event => this.handleMaxLevel(event, this.state.minLevel, this.state.maxLevel)}
// Change function definition like so:
const handleMaxLevel = (event, minLevel, maxLevel) => {
// ...
}
但是,由于您只是在函數(shù)內(nèi)使用狀態(tài)屬性,因此您甚至不需要顯式傳遞它們:
onChange={e => this.handleMaxLevel(e)}
這會(huì)讓你的函數(shù)看起來像這樣:
const handleMaxLevel = event => {
// access state attributes like this:
const {minLevel, maxLevel} = this.state;
// and now you also have access to the event value
this.setState({ maxValue: e.target.value });
}
添加回答
舉報(bào)