import type { FC, PropsWithChildren } from 'react'; import { Switch } from 'antd'; import { createContext, useContext, useEffect, useReducer } from 'react'; import type { ThemeAction, ThemeContextType, ThemeState } from './types'; import { ThemeMode } from './types'; const initialState: ThemeState = { mode: ThemeMode.LIGHT, compact: false, }; const ThemeContext = createContext({ state: initialState, dispatch: () => null, }); const ThemeReducer = (state: ThemeState, action: ThemeAction) => { switch (action.type) { case 'change_mode': return { ...state, mode: action.value }; case 'change_compact': return { ...state, compact: action.value }; default: throw new Error(`Unhandled action type`); } }; export const Theme: FC = ({ children }) => { const [state, dispatch] = useReducer(ThemeReducer, initialState); useEffect(() => { const html = document.getElementsByTagName('html'); console.log(html[0], 'html'); if (html.length > 0) { html[0].classList.remove('light', 'dark'); html[0].classList.add(state.mode); } }, [state.mode]); return {children}; }; export const ThemeConfig = () => { const { state, dispatch } = useContext(ThemeContext); const handleModeChange = () => dispatch({ type: 'change_mode', value: state.mode === ThemeMode.LIGHT ? ThemeMode.DARK : ThemeMode.LIGHT, }); const handleCompactChange = () => dispatch({ type: 'change_compact', value: !state.compact, }); return ( <> ); }; export const ReducerDemo = () => { const { state: { mode, compact }, } = useContext(ThemeContext); return (

useReducer Demo

主题模式: 「{mode === 'dark' ? '暗黑' : '明亮'}」

是否紧凑: 「{compact ? '是' : '否'}」

); };