Last active
November 4, 2018 01:08
-
-
Save third774/e15d581ce05de9cb9eddf1406ee02b5f to your computer and use it in GitHub Desktop.
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
import React, { useReducer, useContext } from "react"; | |
const defaultState = { | |
todos: [], | |
addTodoInputText: "" | |
}; | |
export const StoreContext = React.createContext([defaultState, () => {}]); | |
export function Store({ children }) { | |
const [state, dispatch] = useReducer((state, action) => { | |
switch (action.type) { | |
case "ADD_TODO_UPDATE_TEXT": | |
return { | |
...state, | |
addTodoInputText: action.payload | |
}; | |
default: | |
return state; | |
} | |
}, defaultState); | |
return ( | |
<StoreContext.Provider value={[state, dispatch]}> | |
{children} | |
</StoreContext.Provider> | |
); | |
} | |
export function useStore() { | |
return useContext(StoreContext); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment