Answer the question
In order to leave comments, you need to log in
What is the best way to completely clear the react-redux store when the url changes?
What is the best way to completely clear the react-redux store when the url changes?
Answer the question
In order to leave comments, you need to log in
1. You need to track the url change. The implementation option depends on which router is used, if it is a react-router, then you can use hook
2. When you change the url, you need to dispatch some action, process it, for example, like this
One option is to dispatch an event when the url changes, and listen to this event in the main reducer and set the state to undefined. Then the state equal to undefined will get into the reducers, and default values will be put down.
import appReducer from '../reducers';
const rootReducer = (state, action) => {
if (action.type === 'URL_CHANGED') {
state = undefined;
}
return appReducer(state, action);
};
import { combineReducers } from 'redux';
import someone from './someone';
import someoneT from './someoneT';
export const rootReducer = (state, action) => {
if (action.type === 'URL_CHANGED') {
state = {}
}
return appReducer(state, action)
}
const appReducer = combineReducers({
someone,
someoneT
})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question