在调试过程中,我确认状态正在改变,但是单选按钮并没有在视觉上更新。奇怪的交互:当我包含一个在状态改变后执行的警告时,正确的单选按钮变成了活动的选择,但是一旦我退出警告,它就会恢复,即使状态没有改变。
constructor(props) {
super(props);
this.state = {
algorithmType: 'A*'
}
}
radioUpdated = (event) => {
this.setState({algorithmType: event.currentTarget.value});
}
<form>
<input type='radio'
id='PathFinding_RadioButton_Dijkstras'
name='algorithm'
value='Dijkstras'
checked={this.state.algorithmType == 'Dijkstras' && true}
onChange={this.radioUpdated}></input>
<label for="Dijkstras">Dijkstra's</label><br></br>
<input type='radio'
id='PathFinding_RadioButton_A*'
name='algorithm'
value='A*'
checked={this.state.algorithmType == 'A*'}
onChange={this.radioUpdated}></input>
<label for='A*'>A*</label><br></br>
<input type='radio'
id='PathFinding_RadioButton_D*'
name='algorithm'
value='D*'
onChange={this.radioUpdated}
checked={this.state.algorithmType == 'D*'}
></input>
<label for='D*'>D*</label>
</form>发布于 2021-02-14 13:01:14
我不确定您是否在复制和粘贴代码时犯了错误,您的类组件缺少其呈现功能。
除此之外,您的代码似乎工作得很好,下面是工作示例:
https://codesandbox.io/s/mystifying-gagarin-dh6nz?file=/src/App.js
并且有相同组件的版本,但使用钩子而不是类:
https://codesandbox.io/s/nostalgic-dew-bhy7n?file=/src/App.js
https://stackoverflow.com/questions/66191643
复制相似问题