V
V
Vanya Huk2018-09-27 05:22:40
React
Vanya Huk, 2018-09-27 05:22:40

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

3 answer(s)
S
Sergey Epifanov, 2018-09-27
@kacheleff

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

E
edk55, 2018-09-27
@edk55

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);
};

R
real2210, 2018-09-27
@real2210

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 question

Ask a Question

731 491 924 answers to any question