supasate / connected-react-router

A Redux binding for React Router v4
MIT License
4.73k stars 592 forks source link

How to reset the states with connect-react-router #610

Closed chengr4 closed 1 year ago

chengr4 commented 1 year ago

I would like to clean all global states of Redux store and I found a solution here:

https://stackoverflow.com/a/35641992

But I have no confidence to achieve it together with connected-react-rotuer.

Can anyone help me? Thanks in advance!

chengr4 commented 1 year ago

I found a solution, though there may be a better one.

Instead of passing history as an argument into the createRootReducer function, I simply declare history in the rootReducer file as follows:

// rootReducer.js
import { combineReducers } from 'redux'
import { connectRouter } from 'connected-react-router'
import { createBrowserHistory } from 'history'

export const history = createBrowserHistory()

const createRootReducer = combineReducers({
  router: connectRouter(history),
  ... // rest of your reducers
})
export default createRootReducer

Then, I am able to combine it with the solution from the link above and reset the states.

Thank you all anyway