Created
January 13, 2021 22:38
-
-
Save lautapercuspain/eee9a9db3debbd99359559fe7f45c450 to your computer and use it in GitHub Desktop.
No More Redux Por Favor
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
// store.js | |
import React, {createContext, useReducer} from 'react'; | |
const initialState = {}; | |
const store = createContext(initialState); | |
const { Provider } = store; | |
const StateProvider = ( { children } ) => { | |
const [state, dispatch] = useReducer((state, action) => { | |
switch(action.type) { | |
case 'action description': | |
const newState = // do something with the action | |
return newState; | |
default: | |
throw new Error(); | |
}; | |
}, initialState); | |
return <Provider value={{ state, dispatch }}>{children}</Provider>; | |
}; | |
export { store, StateProvider } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment