React JS - 未捕获的 NotFoundError:无法在“Node”上执行“insertBefore”:

新手上路,请多包涵

我有一个 div 其中另外三个 div 如下附加。通过循环来自 componentWillReceiveProps() 的 api 结果来设置状态值。但是我遇到了一个错误的问题

Uncaught NotFoundError: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node

如果 api 结果为空,则获取

Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

我怎样才能解决这个问题?

 componentWillReceiveProps(nextProps) {
    var sub1 = [], sub2 = [], sub3 = [];
    if(result) {
        result.sub1.map((item, index) => {
            sub1.push(
                <div>
                    <p>{item.name}</p>
                </div>
            )
        })
        result.sub2.map((item, index) => {
            sub2.push(
                <div>
                    <p>{item.name}</p>
                </div>
            )
        })
        result.sub3.map((item, index) => {
            sub3.push(
                <div>
                    <p>{item.name}</p>
                </div>
            )
        })

        this.setState({ subDiv1:sub1, subDiv2:sub2, subDiv3:sub3 })
    }
}

render() {
    return(
        <div className="row top_bar">
            <div className="search left col-xs-5 col-sm-4 col-md-5 col-lg-6">
                <form>
                    <input type="text" id="search_box" name="search_box" placeholder="Search" onKeyUp={this.keyUpFn} />
                </form>
                <div className="div1">
                    { this.state.subDiv1 }
                    { this.state.subDiv2 }
                    { this.state.subDiv3 }
                </div>
            </div>
            <div className="top_right col-xs-7 col-sm-8 col-md-7 col-lg-6">
                <div className="top_outer">
                </div>
            </div>
        </div>
    )
}

原文由 Hareesh 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.1k
1 个回答

有时当用户使用谷歌翻译时会发生这种情况,它会改变文本节点,并且 React 在下一次渲染时中断。更多信息和修复方法: https ://github.com/facebook/react/issues/11538#issuecomment-390386520

您还可以将下一个属性添加到 HTML 标记以禁用 Google 翻译并解决此问题:

<html class="notranslate" translate="no">

原文由 Alexander 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题