Skip to content

Instantly share code, notes, and snippets.

@Rajatgms
Created August 3, 2020 17:37
Show Gist options
  • Save Rajatgms/e408110c316a0fd6272970d35519567e to your computer and use it in GitHub Desktop.
Save Rajatgms/e408110c316a0fd6272970d35519567e to your computer and use it in GitHub Desktop.
Loader
import { createSlice } from '@reduxjs/toolkit';
const loaderSlice = createSlice({
name: 'loader',
initialState: false,
reducers: {
startLoader: (state, action) => action.payload,
},
extraReducers: builder => {
builder
.addMatcher(
action => action.type.endsWith('/pending') && 'requestId' in action.meta,
() => true,
)
.addMatcher(
action => (
action.type.endsWith('/fulfilled') ||
action.type.endsWith('/rejected')) &&
'requestId' in action.meta,
() => false,
)
;
},
});
const { actions, reducer } = loaderSlice;
export const { startLoader } = actions;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment