mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Adding context
This commit is contained in:
46
components/ui/context.tsx
Normal file
46
components/ui/context.tsx
Normal file
@@ -0,0 +1,46 @@
|
||||
import React, { Context, FunctionComponent } from "react";
|
||||
|
||||
export interface ContextType {
|
||||
displaySidebar: boolean;
|
||||
}
|
||||
|
||||
const initialState = {
|
||||
displaySidebar: false,
|
||||
};
|
||||
|
||||
function uiReducer(state, action) {
|
||||
switch (action.type) {
|
||||
case "OPEN_SIDEBAR": {
|
||||
return {
|
||||
...state,
|
||||
displaySidebar: true,
|
||||
};
|
||||
}
|
||||
case "CLOSE_SIDEBAR": {
|
||||
return {
|
||||
...state,
|
||||
displaySidebar: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const UIContext = React.createContext<ContextType>(initialState);
|
||||
UIContext.displayName = "UIContext";
|
||||
|
||||
export const UIProvider: FunctionComponent = (props) => {
|
||||
const [state, dispatch] = React.useReducer(uiReducer, initialState);
|
||||
const value = {
|
||||
...state,
|
||||
dispatch,
|
||||
};
|
||||
return <UIContext.Provider value={value} {...props} />;
|
||||
};
|
||||
|
||||
export const useUI = () => {
|
||||
const context = React.useContext(UIContext);
|
||||
if (context === undefined) {
|
||||
throw new Error(`useUI must be used within a UIProvider`);
|
||||
}
|
||||
return context;
|
||||
};
|
Reference in New Issue
Block a user