redux-Persist 组件中持久化渲染action persit/REHYDRATE 执行时间

redux-persist进行持久化化渲染的时间是在组件reder前吗?还是在组件didMount之后,现在偶现重新进入页面带出上一个页面的数据来,打开控制台发现。redux-persist中间件调用persisit/REHYDRATE action的顺序有的时候是在页面render之间,有的时候在页面didmount更新props

clipboard.png

clipboard.png

阅读 1.7k
1 个回答

新版写法:v5+

<PersistGate loading={null} persistor={persistor}>
  <App/>
</PersistGate>

旧版写法:v4
自己实现一个PersistGate:

class Gate extends Component {
  constructor(props) {
    super(props);
    this.state = {rehydrated: false};
  }
  componentWillMount() {
    const {store, config} = this.props;
    persistStore(store, config, () => {
      this.setState({rehydrated: true});
    });
  }
  render() {
    return (!this.state.rehydrated) ? <div>loading state...</div> : this.props.children
}
//然后用在入口
<Gate store={store} config={config}>
  <App/>
</Gate>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题