Replit Commit
This commit is contained in:
commit
f49f965a42
41 changed files with 32720 additions and 0 deletions
37
src/ctx/StoreContext.jsx
Normal file
37
src/ctx/StoreContext.jsx
Normal file
|
@ -0,0 +1,37 @@
|
|||
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;
|
Loading…
Add table
Add a link
Reference in a new issue