Rematch: Reverting all models to initial state when logging out.

Created on 28 Dec 2018  路  6Comments  路  Source: rematch/rematch

How would you guys do this without putting

'auth/logout': (state) => {
  state = null
  return { ...state }
}

in every model?

Most helpful comment

Looking to how this would be done in pure Redux is probably needlessly complicated.

@gungdeaditya, this is the solution I'm using, and I think it is fairly elegant / concise. Just init the store with the rootReducer you need:

const store = init({
  plugins,
  models,
  redux: {
    rootReducers: {
      RESET_APP: (state, action) => undefined,
    },
    devtoolOptions: {
      disabled: process.env.NODE_ENV === 'production',
    },
  },
});

And then call RESET_APP whenever you need to by sending a message, ala classic Redux:

dispatch({ type: 'RESET_APP' })

All 6 comments

mark

You can create a rootReducer, rematch works perfectly with Redux things.
Here you have an example using Redux: https://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store/35641992#35641992.
Having this as a plugin would be so nice

You can also re-use a base reducer per model:

import { resetsOnLogout } from './authUtils'

const model = {
  baseReducer: resetsOnLogout,
  ...
}

thank you gentlemen

I'm sorry, can you please providing snippet example of doing resetting state with baseReducer ?

Looking to how this would be done in pure Redux is probably needlessly complicated.

@gungdeaditya, this is the solution I'm using, and I think it is fairly elegant / concise. Just init the store with the rootReducer you need:

const store = init({
  plugins,
  models,
  redux: {
    rootReducers: {
      RESET_APP: (state, action) => undefined,
    },
    devtoolOptions: {
      disabled: process.env.NODE_ENV === 'production',
    },
  },
});

And then call RESET_APP whenever you need to by sending a message, ala classic Redux:

dispatch({ type: 'RESET_APP' })
Was this page helpful?
0 / 5 - 0 ratings

Related issues

sospedra picture sospedra  路  3Comments

miraage picture miraage  路  4Comments

ShMcK picture ShMcK  路  6Comments

alexicum picture alexicum  路  5Comments

777PolarFox777 picture 777PolarFox777  路  3Comments