Untitled
unknown
typescript
2 years ago
670 B
44
Indexable
import { createContext, ReactNode, useMemo, useState } from "react"; export interface ITheme { switchTheme: () => void; theme: string; }; interface IProps { children: ReactNode; } export const ThemeContext = createContext<ITheme | null>(null); export const ThemeContextProvider = ({ children }: IProps) => { const [theme, setTheme] = useState<string>("light") const switchTheme = () => { setTheme(prev => prev === "light" ? "dark" : "light"); } const contextValues = useMemo( () => ({ theme, switchTheme }), [theme] ); return ( <ThemeContext.Provider value={contextValues}> {children} </ThemeContext.Provider> ); };
Editor is loading...