【发布时间】:2019-04-16 19:37:23
【问题描述】:
我有一个父组件和两个子组件。一场成功一场失败 我需要在页面加载时基于异步调用显示这些组件。 所以我在页面componentDidMount上做了异步调用。但这会导致双重渲染。
constructor(props) {
super(props)
this.state = {
showSuccessPage: false
}
}
componentDidMount() {
ActiveAccount.fetchActiveAccount(this.handleSuccess, this.handleFailure)
}
handleSuccess() {
this.setState({
showSuccessPage: true
)}
}
...
render() {
...
return (
{showSuccessPage && <SuccessPage />}
{!showSuccessPage && <FailurePage />}
)
}
它总是先渲染失败页面,然后更新渲染成功页面。如何防止双重渲染?
【问题讨论】:
-
您可以将渲染逻辑缩短为
showSuccessPage ? <SuccessPage /> : <FailurePage />。
标签: reactjs asynchronous call mount