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 })
})
}
}