【发布时间】:2017-10-30 12:46:28
【问题描述】:
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-saga 及其选择器
标签: javascript reactjs redux react-redux