首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么组件状态会更新,但视觉效果不会更新?

为什么组件状态会更新,但视觉效果不会更新?
EN

Stack Overflow用户
提问于 2021-02-14 09:29:34
回答 1查看 19关注 0票数 0

在调试过程中,我确认状态正在改变,但是单选按钮并没有在视觉上更新。奇怪的交互:当我包含一个在状态改变后执行的警告时,正确的单选按钮变成了活动的选择,但是一旦我退出警告,它就会恢复,即使状态没有改变。

代码语言:javascript
复制
  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>
EN

回答 1

Stack Overflow用户

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66191643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档