qualiteer/src/ctx/StoreContext.jsx

38 lines
799 B
React
Raw Normal View History

2022-05-05 12:35:47 +00:00
import React, { useReducer, createContext, useMemo } from "react";
const StoreContext = createContext();
const ACTIONS = {
UPDATE: "u",
};
const initialState = {};
const reducer = (state, action) => {
// Actions
switch (action.type) {
case ACTIONS.UPDATE:
return { ...state, ...store };
default:
return state;
}
};
export const StoreProvider = ({ children }) => {
const [state, dispatch] = useReducer(reducer, initialState);
const context = {
state,
dispatch,
updateStore: (store) => dispatch(state, { type: ACTIONS.UPDATE, store }),
};
const contextValue = useMemo(() => context, [state, dispatch]);
return (
<StoreContext.Provider value={contextValue}>
{children}
</StoreContext.Provider>
);
};
export default StoreContext;