ReactJS-超出最大更新深度错误
我面临一个错误: 超过最大更新深度。当一个组件 在componentWillUpdate或 组件更新。React将嵌套更新的数量限制为 防止无限循环 守则如下:ReactJS-超出最大更新深度错误,reactjs,Reactjs,我面临一个错误: 超过最大更新深度。当一个组件 在componentWillUpdate或 组件更新。React将嵌套更新的数量限制为 防止无限循环 守则如下: componentDidUpdate(){ 这是。_updateLocalStorage(); 如果(this.state.searchValue!==“”){ 这是我的国家({ filteredNotes:this.state.notes.filter( 注=> 注.text .toLowerCase() .indexOf(this.
componentDidUpdate(){
这是。_updateLocalStorage();
如果(this.state.searchValue!==“”){
这是我的国家({
filteredNotes:this.state.notes.filter(
注=>
注.text
.toLowerCase()
.indexOf(this.state.searchValue.toLowerCase())!=-1
)
});
}否则{
这是我的国家({
filteredNotes:this.state.notes
});
}
}
如果状态发生更改,则每次都会调用组件diddupdate 因此,您可能需要小心地在内部使用
setState
对那些
setState
严格要求您的条件。也许是这样的:
一些注意事项:
- 对象比较
- 值/地址引用
如果状态发生更改,则每次都会调用组件diddupdate 因此,您可能需要小心地在内部使用
setState
对那些
setState
严格要求您的条件。也许是这样的:
一些注意事项:
- 对象比较
- 值/地址引用
这就是正在发生的事情:
中,this.state.searchValue
获取truthy-它更新状态componentDidUpdate(prevProps,prevState)
这就是正在发生的事情:
中,this.state.searchValue
获取truthy-它更新状态componentDidUpdate(prevProps,prevState)
如果在主状态调用之前两个状态相等,则可以通过放置空比较来修复此问题。此外,我还改变了else的状况
componentDidUpdate(prevProps,prevState){
这是。_updateLocalStorage();
如果(this.state.searchValue!==“”){
这是我的国家({
filteredNotes:this.state.notes.filter(
注=>
注.text
.toLowerCase()
.indexOf(this.state.searchValue.toLowerCase())!=-1
)
});
}else if(prevState.filteredNotes==此.state.filteredNotes){
//无所事事
}else if(this.state.searchValue==“”){
这是我的国家({
filteredNotes:this.state.notes
});
}
}
如果在主状态调用之前两个状态相等,则可以通过放置空比较来修复此问题。此外,我还改变了else的状况
componentDidUpdate(prevProps,prevState){
这是。_updateLocalStorage();
如果(this.state.searchValue!==“”){
这是我的国家({
filteredNotes:this.state.notes.filter(
注=>
注.text
.toLowerCase()
.indexOf(this.state.searchValue.toLowerCase())!=-1
)
});
}else if(prevState.filteredNotes==此.state.filteredNotes){
//无所事事
}else if(this.state.searchValue==“”){
这是我的国家({
filteredNotes:this.state.notes
});
}
}
您好,需要帮助吗?因为我刚刚看到你撤销了accept,如果你面临进一步的问题,请随时进行更新。@keikai它正在工作,但当我进行更改时,它不会立即反映出来。只有我必须在每次更改完成时刷新浏览器。所以我想我被困在那里了。状态更新是异步的,代码中有另一个错误,但我确信它与这个错误无关。检查文档:嗨,需要帮助吗?因为我刚刚看到你撤销了accept,如果你面临进一步的问题,请随时进行更新。@keikai它正在工作,但当我进行更改时,它不会立即反映出来。只有我必须在每次更改完成时刷新浏览器。所以我想我被困在那里了。状态更新是异步的,代码中有另一个错误,但我确信它与这个错误无关。检查文档:
componentDidUpdate(pervProps, prevState) {
if (prevState.filteredNotes !== this.state.filteredNotes) {
this._updateLocalStorage();
if (this.state.searchValue !== "") {
this.setState({
filteredNotes: this.state.notes.filter(
note =>
note.text
.toLowerCase()
.indexOf(this.state.searchValue.toLowerCase()) !== -1
)
});
} else {
this.setState({
filteredNotes: this.state.notes
});
}
}
}
if(this.state.searchValue !== "" && this.state.searchValue !== prevState.searchValue) {...}