ComponentDidMount() 在组件安装时触发,包括在服务器端渲染后进行水合时。
我在网上找到的解决方案之一是检查该州是否有数据;然而,这需要在每个组件上包含大量代码。还有哪些其他解决方案?
componentDidMount() {
// if rendered initially, we already have data from the server
// but when navigated to in the client, we need to fetch
if (!this.state.data) {
this.constructor.fetchData(this.props.match).then(data => {
this.setState({ data })
})
}
}
我找到了替代解决方案。在我的 Redux 存储中,我保留当前页面的 URL。因此,在导航上,我能够执行以下操作:
componentDidMount() {
const { url, match } = this.props;
if (url !== match.url) {
fetchData(match.path);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)