【发布时间】:2021-03-26 17:32:24
【问题描述】:
我有一个div,其中另外三个divs 附加如下。状态值是通过循环来自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 结果为 null 则获取
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>
)
}
【问题讨论】:
-
您能提供一个完整的可运行示例吗?此消息听起来像一个错误。
-
这只是我项目的一小部分。现在将其创建为可运行的示例并不困难。请不要难过
-
我认为应该在某处使用像
insertbefore这样的东西。我不知道在哪里使用它。你有什么想法吗? -
我最好的猜测是你有一些与 React 生成的 DOM 混淆的代码,当 React 稍后尝试更新它时,它找不到节点。但是没有看到你的项目就很难说。通常,您显示的代码很奇怪。你真的不应该像这样使用 React。相反,渲染应该在 render 方法中完成。
-
这是从
componentWillReceiveProps循环和设置状态并在渲染方法中使用状态值的错误方法吗?
标签: javascript reactjs redux