【发布时间】:2019-12-12 19:10:40
【问题描述】:
我想要做的是我想要取消选中的功能。例如:当我选择奇数时,只读输入的背景变为黄色,当我再次单击时,它变为白色,但它不会取消选择该奇数的比率。当背景变为黄色时,e.target.value 被添加到所选状态,当它再次变为白色时,我找不到从状态中删除目标值的方法。我想为了更好地理解你可能需要检查 github(https://github.com/Elvaleryn/choosebet) 中的代码。这是一张图片:img1。注意:我很清楚 .splice() 方法。
//state for selecting how much money user will put
const [betMoney, setBetMoney] = useState(3)
//state for the odd value
const [chosen, setChosen] = useState([1])
//basically a data base for games
const [games, setGames] = useState([])
//getting data from server
useEffect(() => {
gameService
.fetchGames()
.then(response => {
setGames(response.data)
})
}, [])
//to pick the chosen odd
const handleOptionChange = (game, option) => {
game[option] = !game[option]
setGames([...games])
}
const handleMoneyChange = (event) => {
setBetMoney(event.target.value)
}
//when odd is chosen it activates the handle option change to apply background color
const chooseOdd = (e, person, option) => {
//when odd is chosen it activates the handle option change to apply background color
handleOptionChange(person, option)
const index = e.target.value
setChosen(chosen.concat(index))
}
const ratioTotal = chosen.reduce((a, b) => a * b)
const result = parseFloat(ratioTotal * betMoney).toFixed(2)
【问题讨论】:
-
另外,don't mutate the state 就像你对
game[option] = !game[option]所做的那样。
标签: javascript reactjs react-hooks