【发布时间】:2019-04-04 20:46:31
【问题描述】:
我有 2 条路由 /dashboard 和 /battery,在 /dashboard 上我在 componentWillMount() 上调用我的 API,然后一旦我收到使用 redux 的响应,我就会使用 componentWillReceiveProps() 来更新我的状态及其工作正常,但是如果我使用 react-router-dom 导航到 /battery,然后想返回 /dashboard,我该如何防止 API 调用,因为数据已经存在于 redux-store 中。
// To go to the /battery I do
history.push('/main/battery')
// To go to the /dashboard I do
history.push('/main/dashboard')
// Dashboard Component (/main/dashboard)
componentWillMount() {
this._callAPI()
}
componentWillReceiveProps(nextProps) {
// update state
}
【问题讨论】:
-
在你的 componentWillMount 中向 Redux 发送一个动作,并在 reducer 中检查你的状态的正确部分是否存在。如果没有,请从 reducer 调用您的 api