Issue: The game
state 从混合的室内和室外输入获取状态,并且当您尝试在前一个状态的户外游戏中传播时game
字符串,它们被解释为字符数组,因为它们是可迭代的。它们一次一个地传播。
预期的:Outdoor Games 单选按钮结果值字符串之一OR室内游戏检查值 ID 字符串数组。
解决方案:添加第二个状态来跟踪您正在与哪个“选项卡”交互,即室内/室外。 (IE。使用事件处理程序被调用的事实)
this.state = {
indoor: null,
game: ""
};
添加切换indoor
与“室外”单选按钮交互时为 false。
handleRadioSelect = (e, { value }) => {
this.setState({ indoor: false, game: value });
};
然后使用“室内”复选框处理程序中的检查来查看是否already与室内选项互动or如果用户界面刚刚切换自室外侧。
handleCheck = id => {
const { game, indoor } = this.state;
if (!indoor) {
// switching from outdoor games
// set indoor and initialize array
this.setState({ indoor: true, game: [id] });
} else {
// filter array as per usual
const result = game.includes(id)
? game.filter(x => x !== id)
: [...game, id];
this.setState({ game: result });
}
};