Skip to content

Instantly share code, notes, and snippets.

@StoneyEagle
Created September 14, 2022 01:00
Show Gist options
  • Save StoneyEagle/1607df9ff835826a521ce665e9467740 to your computer and use it in GitHub Desktop.
Save StoneyEagle/1607df9ff835826a521ce665e9467740 to your computer and use it in GitHub Desktop.
Redux base template
// ./redux/content/actions.js
import { store } from '..';
import content from './content';
export const setLoading = (payload) => store.dispatch(content.actions.setContentLoading(payload));
export const setError = (payload) => store.dispatch(content.actions.setContentError(payload));
export const setInfo = (payload) => store.dispatch(content.actions.setInfo(payload));
// ./redux/content/content.js
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
const initialState = {
info: {},
data: null,
error: null,
loading: false,
};
const content = createSlice({
name: 'content',
initialState: initialState,
reducers: {
setLoading: (state, action) => {
state.loading = action.payload;
},
setError: (state, action) => {
state.error = action.payload;
},
setInfo: (state, action) => {
state.info = action.payload;
},
},
});
export default content;
// ./redux/content/index.js
export * from './content';
export { default } from './content';
// ./redux/index.js
/* dependencies
redux
redux-persist
@reduxjs/toolkit
*/
import storage from 'redux-persist/lib/storage';
import { combineReducers, legacy_createStore as createStore } from 'redux';
import { persistReducer } from 'redux-persist';
import content from './content';
export const appReducer = combineReducers({
content: content.reducer,
});
export type AppState = ReturnType;
const appPersistConfig: PersistConfig = {
key: 'app',
keyPrefix: '',
storage,
debug: true,
};
export const store = createStore(
persistReducer(appPersistConfig, appReducer),
window.__REDUX_DEVTOOLS_EXTENSION__ &&
window.__REDUX_DEVTOOLS_EXTENSION__({
trace: true,
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment