javascript - React 模态 - 不变违规

标签 javascript twitter-bootstrap reactjs

我有一个使用 Bootstrap 模式的 React 应用程序:

<div id="newListModal" className="modal fade" role="dialog">
    <div className="modal-dialog">

        <div className="modal-content">
            <div className="modal-header">
                <button type="button" className="close" data-dismiss="modal">&times;</button>
                <h4 className="modal-title">Create new list</h4>
            </div>

            <div className="modal-body">
                <div>
                    <p>Select category</p>
                    <select class="form-control" onChange={this.handleSelect}>
                        {categoryDesc}
                    </select>
                    {this.state.categorySelected == true
                        ?
                        <div>
                            <p>Select classification</p>
                            <select class="form-control">
                                {this.state.classificationDesc}
                            </select>
                        </div>
                        :
                        <div></div>}
                </div>
            </div>

            <div className="modal-footer">
                <button type="button" className="btn btn-default" data-dismiss="modal" onClick={this.closeModal}>Close</button>
            </div>
        </div>

    </div>
</div>

我有两个下拉菜单。我第一次选择它们,一切都很好。 但是,如果我尝试再次选择第一个,我会得到:

Uncaught Error: Invariant Violation: findComponentRoot(..., .0.2.1.0.0.1.0.2.1.0.0): Unable to find element. This probably means the DOM was unexpectedly mutated (e.g., by the browser), usually due to forgetting a <tbody> when using tables, nesting tags like <form>, <p>, or <a>, or using non-SVG elements in an <svg> parent. Try inspecting the child nodes of the element with React ID ``

Uncaught Error: Invariant Violation: findComponentRoot(..., ...$110): Unable to find element. This probably means the DOM was unexpectedly mutated是一个类似的问题,但我不使用任何表格

关于导致此错误的原因有什么想法吗?

最佳答案

您需要为所有动态子项传递一个唯一的 key 属性

   <p>Select category</p>
   <select class="form-control" onChange={this.handleSelect}>
    {this.props.categories.map(function (elem, index) { 
       return (
        <option value={index} key={"category" + index}>
          <a href="#">{elem.description}</a>
        </option>
       );
    })}
   </select>

The situation gets more complicated when the children are shuffled around (as in search results) or if new components are added onto the front of the list (as in streams). In these cases where the identity and state of each child must be maintained across render passes, you can uniquely identify each child by assigning it a key

有关更多信息,请参阅 https://facebook.github.io/react/docs/multiple-components.html#dynamic-children

关于javascript - React 模态 - 不变违规,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33692527/

相关文章:

javascript - react 要求.确保。无法读取 require 加载的组件的 props

javascript - Jquery 结果在其相对 div 中

javascript - 为什么带有 Node.JS 工具的 Visual Studio 2017 无法运行/调试 node.js 项目?

javascript - 如何将带有 URL 的文本作为参数安全地传递给 CodeIgniter Controller ?

css - 如何更改工具提示的背景颜色?

javascript - React Native : Redux-persist, 操作结果未持久化

javascript - 在 div 标签附近出现错误

javascript - 获取输入值并以 "|"分隔

javascript - 如何在 Bootstrap 中为 Accordion 添加加号或减号? Accordion 关闭时显示加号, Accordion 打开时显示减号

javascript - select2 jquery x-editable 未设置值 - 单个值