How to reset the state of a Redux store?

前端 未结 30 2319
陌清茗
陌清茗 2020-11-22 06:20

I am using Redux for state management.
How do I reset the store to its initial state?

For example, let’s say I have two user accounts (u1 and

30条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 06:46

    A quick and easy option which worked for me was using redux-reset . Which was straightforward and also has some advanced options, for larger apps.

    Setup in create store

    import reduxReset from 'redux-reset'
    ...
    const enHanceCreateStore = compose(
    applyMiddleware(...),
    reduxReset()  // Will use 'RESET' as default action.type to trigger reset
    )(createStore)
    const store = enHanceCreateStore(reducers)
    

    Dispatch your 'reset' in your logout function

    store.dispatch({
    type: 'RESET'
    })
    

    Hope this helps

提交回复
热议问题