Skip to content

Instantly share code, notes, and snippets.

@third774
Last active November 4, 2018 01:08
Show Gist options
  • Save third774/e15d581ce05de9cb9eddf1406ee02b5f to your computer and use it in GitHub Desktop.
Save third774/e15d581ce05de9cb9eddf1406ee02b5f to your computer and use it in GitHub Desktop.
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