react useContext
2022年7月30日小于 1 分钟
创建 ThemeContext.js
export const ThemeContext = React.createContext({
theme: "auto",
setTheme: (newTheme: string) => {},
});
在外层中:
function MyApp({ Component, pageProps }: AppProps) {
const [theme, setTheme] = useState("auto");
return (
<ThemeContext.Provider value={{ theme: theme, setTheme }}>
<Component {...pageProps} />
</ThemeContext.Provider>
);
}
在内层:
const { theme, setTheme } = useContext(ThemeContext);
Loading...