One way to do that would be to write a root reducer in your application.(一种方法是在您的应用程序中编写根减少器。)
The root reducer would normally delegate handling the action to the reducer generated by combineReducers()
.(根减速器通常会将处理操作委派给combineReducers()
生成的减速器。)
However, whenever it receives USER_LOGOUT
action, it returns the initial state all over again.(但是,每当收到USER_LOGOUT
操作时,它将再次返回初始状态。)
For example, if your root reducer looked like this:(例如,如果您的根减速器如下所示:)
const rootReducer = combineReducers({
/* your app’s top-level reducers */
})
You can rename it to appReducer
and write a new rootReducer
delegating to it:(您可以将其重命名为appReducer
并编写一个新的授权给它的rootReducer
:)
const appReducer = combineReducers({
/* your app’s top-level reducers */
})
const rootReducer = (state, action) => {
return appReducer(state, action)
}
Now we just need to teach the new rootReducer
to return the initial state after USER_LOGOUT
action.(现在,我们只需要教导新的rootReducer
在USER_LOGOUT
操作之后返回初始状态。)
As we know, reducers are supposed to return the initial state when they are called with undefined
as the first argument, no matter the action.(众所周知,无论使用什么操作,都必须在以undefined
作为第一个参数的情况下调用reducer来返回初始状态。) Let's use this fact to conditionally strip the accumulated state
as we pass it to appReducer
:(让我们利用这一事实在将累积state
传递给appReducer
地appReducer
累积state
:)
const rootReducer = (state, action) => {
if (action.type === 'USER_LOGOUT') {
state = undefined
}
return appReducer(state, action)
}
Now, whenever USER_LOGOUT
fires, all reducers will be initialized anew.(现在,每当USER_LOGOUT
触发时,所有的reducer都会重新初始化。)
They can also return something different than they did initially if they want to because they can check action.type
as well.(如果愿意,他们还可以返回与最初不同的东西,因为他们也可以检查action.type
。)
To reiterate, the full new code looks like this:(重申一下,完整的新代码如下所示:)
const appReducer = combineReducers({
/* your app’s top-level reducers */
})
const rootReducer = (state, action) => {
if (action.type === 'USER_LOGOUT') {
state = undefined
}
return appReducer(state, action)
}
Note that I'm not mutating the state here, I am merely reassigning the reference of a local variable called state
before passing it to another function.(请注意,我这里没有变异的状态,我只是重新分配参考称为局部变量的state
它传递给另一个函数之前。)
Mutating a state object would be a violation of Redux principles.(更改状态对象将违反Redux原则。)
In case you are using redux-persist , you may also need to clean your storage.(如果您使用redux-persist ,则可能还需要清理存储。)
Redux-persist keeps a copy of your state in a storage engine, and the state copy will be loaded from there on refresh.(Redux-persist会将状态副本保存在存储引擎中,状态副本将在刷新时从那里加载。)
First, you need to import the appropriate storage engine and then, to parse the state before setting it to undefined
and clean each storage state key.(首先,您需要导入适当的存储引擎 ,然后在将状态设置为undefined
之前解析状态,并清除每个存储状态键。)
const rootReducer = (state, action) => {
if (action.type === SIGNOUT_REQUEST) {
// for all keys defined in your persistConfig(s)
storage.removeItem('persist:root')
// storage.removeItem('persist:otherKey')
state = undefined;
}
return appReducer(state, action);
};