javascript - componentWillUpdate 被无限调用,即使有条件
问题描述
我有一个名为ListOfItems.js的类组件,它列出了一堆对象,如下所示:
class ListOfItems extends Component {
construction (props) {
super(props);
this.state = {
objectList: []
}
}
}
// This gets the list of objects to display
componentDidMount() {
this.getObjectList();
}
componentDidUpdate(_prevProps, prevState) {
if( this.state.ObjectList !== prevState.ObjectList) {
// this is called infinitely
console.log("entered");
this.getObjectList();
}
}
getObjectList = () => {
const input = { id_for_this_list: id }
fetch( connectToApi, {
method: "PUT",
headers: { //stuff}
body: JSON.stringify(input)
})
.then(res => //)
.then(result => //)
.catch(err=>{console.error(err)});
}
在Object.js(ListOfItems 中显示的每个对象的文件)内部,我有一个删除功能,如果用户选择对象上的“x”图标,该对象将从列表中删除。ListOfItems.js 中的 Object.js 实例如下所示:
{this.state.objectList.map(item) => {
<Object
objectList={this.objectList}
data={item}
//few other props here
>
}
一旦我输入这个 ListOfItems.js,componentWillUpdate 就会被无限调用,即使是有条件的。我希望它只被调用一次——当用户在 ListOfItems 页面上删除一个对象时。
解决方案
推荐阅读
- c# - Entity Framework Core - 尝试从迁移中创建数据库,但遇到“Microsoft.Extensions.Hosting services”问题
- javascript - AngularJs 无法在 localhost 的 url 中获取带点的路由
- javascript - OMDb API 需要帮助在使用 AXIOS 进行电影搜索后获取 /results 页面
- java - ToolProvider.getSystemJavaCompiler 返回 null 但我确实安装了 JDK
- postgresql - 从文件执行 psql 查询与通过 bash 传递它
- oauth-2.0 - Microsoft Graph 的 OAuth2 客户端凭据流
- azure-data-explorer - 在 azure kusto 中将带有分隔符的列字符串拆分为单独的列
- java - Spring Boot 使用基本身份验证保护执行器端点,同时使用 Oath 保护其他端点
- javascript - 请求标头值在 Chrome 中转换为大写
- javascript - 退出全屏后地址栏不出现 - Chrome 85 Mobile