Created
September 14, 2022 01:00
-
-
Save StoneyEagle/1607df9ff835826a521ce665e9467740 to your computer and use it in GitHub Desktop.
Redux base template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./redux/content/index.js | |
export * from './content'; | |
export { default } from './content'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./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