javascript - 无限获取循环
问题描述
一旦我打开我的 DocumentViewer 获取无限循环,我看不出为什么会发生这种情况,有人可能会看到问题出在哪里,它与状态和道具有关,但我不知道为什么,知道什么会很棒是问题以及如何解决它
class GeneralDocPresenter extends React.Component {
state = {
metaInfoDocs: [],
docs: [],
loading: false
};
updateDoc = () => {
this.props.selectedDocsStore.clear();
this.props.selectedDocsStore.setViewDocId(0);
this.setState({ loading: true });
this.props
.fetchMetaDocs()
.then((r) => this.setState({ metaInfoDocs: r.data, loading: false }))
.catch((err) => {
this.setState({ loading: false });
errorWithMessage("Could not load documents");
});
this.props.eventManager.on("viewDoc", (doc) => {
this.loadDocuments(doc.id);
});
};
componentDidUpdate(prevProps, prevState, snapshot) {
this.updateDoc()
}
componentDidMount() {
this.updateDoc()
}
render() {
return <Translation>
{(t) => {
if (this.state.loading) {
return (
<div style={{display: 'flex', justifyContent: 'center'}}>
<Spin size={"medium"}/>
</div>
)
}
if (this.state.metaInfoDocs.length === 0) {
return (
<div style={{display: 'flex', justifyContent: 'center'}}>
<NoDocumentsAlert><div dangerouslySetInnerHTML={{__html: t('noDocuments')}}/></NoDocumentsAlert>
</div>
)
}
return (
<DocViewWrapper docs={this.state.docs}
metaInfoDocs={this.state.metaInfoDocs.map(doc => {
return {...doc, type: this.props.type}
})}
eventManager={this.props.eventManager}
settings={this.props.settings}
childComponents={this.props.childComponents}
/>
)
}}
</Translation>
}
loadDocuments(id) {
this.props.loadDocument(id).then(r => {
this.setState({
docs: r.data
})
});
}
}
解决方案
尝试ComponentDidUpdate
从
componentDidUpdate(prevProps, prevState, snapshot) {
this.updateDoc()
}
到
componentDidUpdate(prevProps, prevState, snapshot) {
if(this.props !== prevProps){
this.updateDoc()
}
}
您可以更具体地指定 didUpdate 要检查的内容,而不是检查完整的道具更改。
推荐阅读
- php - 当我将 mysqli_fetch_assoc 与 HTML 表单一起使用时,它正在发送第一行
- python-3.x - ErrUchIsDown:您的进程因使用过多资源而被杀死
- django - Django中外键的实现
- android - Android 数据绑定:在生成的数据绑定文件中找不到 ...BindingImpl
- c++ - CComSafeArray to Tab-Separated Values 提高速度的建议
- arrays - 使用多个过滤器附加查询字符串
- docker - docker build --tag= 未正确设置标签,存储库
- numpy - 为什么内置的岭回归不能以任何合理的方式预测数据的真实值?
- php - 抓取网页的问题。(PHP)
- java - org.jboss.weld.exceptions.DefinitionException:在 payara/glassfish 中部署时出现 WELD-001113