简体   繁体   English

React DVA JS 框架:重置整个状态应用程序

[英]React DVA JS Framework: Reset entire state application

I'm using DVA JS Framework ( https://github.com/dvajs/dva ) in a React Project.我在 React 项目中使用 DVA JS 框架 ( https://github.com/dvajs/dva )。
I have a login/logout feature in this web app.我在这个网络应用程序中有一个登录/注销功能。

The problem:问题:
When I logout the state stays the same.当我注销时,状态保持不变。 I need to reset the entire state without refreshing the page.我需要在不刷新页面的情况下重置整个状态。

Is there a way to do it in DVA Framework?有没有办法在 DVA 框架中做到这一点?

In DVA documentation ( https://github.com/dvajs/dva/blob/master/docs/API.md ) there are many ways to access different things in the app.在 DVA 文档 ( https://github.com/dvajs/dva/blob/master/docs/API.md ) 中有很多方法可以访问应用程序中的不同内容。

const app = dva({
  initialState,
  onAction,
  onStateChange,
  onReducer,
  onEffect,
});

Is there a way to reset the state inside these configurations?有没有办法重置这些配置中的状态?

How are you logging out?你怎么退出? Using dva you need to use the model to do that, in order to trigger an "effect" saga.使用 dva 您需要使用模型来做到这一点,以触发“效果”传奇。 You can then trigger a state change to reset the values you need.然后您可以触发状态更改以重置您需要的值。 For example:例如:

...
effects {
  *logout({ payload }, { call, put }) {
    const confirmation = yield call(logout, {});
    if (confirmation.success) {
      yield put({ type: 'resetToDefaults' });
    } else {
      throw (confirmation);
    }
  },
},
reducers {
  resetToDefaults(state) {
    return {
      ...state,
      //Modify your required entries to the default values
    };
  },
}

我使用 2 个状态,第一个是我的实际状态,第二个是我调用 resetToDefaults 时将执行的 defaultState。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM