Last active
April 25, 2020 15:30
-
-
Save mattlockyer/248c3fbeb19824140e09de133e59be66 to your computer and use it in GitHub Desktop.
A waaay simpler redux, redux-thunk friendly setup for reducer, state and actions
This file contains hidden or 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
import { getReducer, getState } from '../util/redux-util' | |
//default state | |
const defaultState = { | |
toggle: false | |
} | |
//reducer | |
const type = 'appReducer' | |
export const appReducer = getReducer(type, defaultState) | |
export const appState = getState(type) | |
//actions | |
export const onToggle = (val) => async (dispatch, getState) => { | |
dispatch({ type, toggle: val }) | |
} |
This file contains hidden or 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
//generic reducer for updating state | |
export const getReducer = (type, defaultState) => { | |
return (state = defaultState, action) => { | |
if (action.type !== type) return state | |
delete action.type | |
return { ...state, ...action } | |
} | |
} | |
//generic function for returning state based on keys found in defaultState | |
export const getState = (type) => ({ [type]: { ...state } }) => state |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment