Files
nym/ts-packages/react-webpack-with-theme-example/src/context/index.tsx
T
Fouad 2eee5195cc React / React DOM / Types upgrade to version 18 (#2830)
* new react and reactdom packages in wallet

* new react and reactdom packages in root

* new react and reactdom packages in nym connect

* new react and reactdom packages in root

* update react and reactdom for explorer

* react and react-dom upgrade for ts-packages

remove unused import

fix linting error

* use custom FC typing

move typings folder

* fix type error
2023-01-12 17:15:31 +00:00

35 lines
902 B
TypeScript

import { PaletteMode } from '@mui/material';
import * as React from 'react';
interface State {
mode: PaletteMode;
toggleMode: () => void;
}
const AppContext = React.createContext<State | undefined>(undefined);
export const useAppContext = (): State => {
const context = React.useContext<State | undefined>(AppContext);
if (!context) {
throw new Error('Please include a `import { AppContextProvider } from "./context"` before using this hook');
}
return context;
};
export const AppContextProvider: FCWithChildren = ({ children }) => {
// light/dark mode
const [mode, setMode] = React.useState<PaletteMode>('dark');
const value = React.useMemo<State>(
() => ({
mode,
toggleMode: () => setMode((prevMode) => (prevMode !== 'light' ? 'light' : 'dark')),
}),
[mode],
);
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
};