rtk-incubator / rtk-query

Data fetching and caching addon for Redux Toolkit
https://redux-toolkit.js.org/rtk-query/overview
MIT License
626 stars 31 forks source link

How to to clear all caches from services on user logout ? #162

Closed quentinbrohan closed 3 years ago

quentinbrohan commented 3 years ago

Hell-O,

I can't find a way to clear all caches from 2 services (tripApi, userApi) when a user logout. I would like to clear everything (queries, mutations, etc) when the user click on the logout button as well as clearing my RTK slice.

I tried to create a mutation with invalidating parameters and playing with onStart but without success, and I've found nothing about it on internet and in the typing.

Any tips? Thanks

msutkowski commented 3 years ago

@quentinbrohan This would be the same process you'd do for clearing redux state in a normal app.

As a quick example, you could do something like this, which would clear your store.

import { combineReducers } from '@reduxjs/toolkit';

import { api } from './yourService';
import { logout } from '../features/auth';

const appReducer = combineReducers({
  [api.reducerPath]: api.reducer,
});

const rootReducer = (state, action) => {
  if (logout.match(action)) {
    state = undefined;
  }

  return appReducer(state, action);
};

export default rootReducer;
quentinbrohan commented 3 years ago

@msutkowski Thanks, perfect and simple !