|
| 1 | +import { |
| 2 | + useState, |
| 3 | + useEffect, |
| 4 | + useContext, |
| 5 | + useReducer, |
| 6 | + useMemo, |
| 7 | + useCallback, |
| 8 | + useRef, |
| 9 | + createContext, |
| 10 | + type FC, |
| 11 | + type ReactNode, |
| 12 | +} from 'react' |
| 13 | + |
| 14 | +// ─── useContext Demo ────────────────────────────────────────────────────────── |
| 15 | +interface ThemeContextValue { |
| 16 | + theme: 'light' | 'dark' |
| 17 | + toggleTheme: () => void |
| 18 | +} |
| 19 | + |
| 20 | +const ThemeContext = createContext<ThemeContextValue>({ |
| 21 | + theme: 'light', |
| 22 | + toggleTheme: () => {}, |
| 23 | +}) |
| 24 | + |
| 25 | +function ThemeProvider({ children }: { children: ReactNode }) { |
| 26 | + const [theme, setTheme] = useState<'light' | 'dark'>('light') |
| 27 | + const toggleTheme = useCallback(() => { |
| 28 | + setTheme(prev => (prev === 'light' ? 'dark' : 'light')) |
| 29 | + }, []) |
| 30 | + return ( |
| 31 | + <ThemeContext.Provider value={{ theme, toggleTheme }}> |
| 32 | + {children} |
| 33 | + </ThemeContext.Provider> |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +// ─── useReducer Demo ────────────────────────────────────────────────────────── |
| 38 | +interface CounterState { |
| 39 | + count: number |
| 40 | + step: number |
| 41 | +} |
| 42 | + |
| 43 | +type CounterAction = |
| 44 | + | { type: 'increment' } |
| 45 | + | { type: 'decrement' } |
| 46 | + | { type: 'reset' } |
| 47 | + | { type: 'setStep'; payload: number } |
| 48 | + |
| 49 | +function counterReducer(state: CounterState, action: CounterAction): CounterState { |
| 50 | + switch (action.type) { |
| 51 | + case 'increment': return { ...state, count: state.count + state.step } |
| 52 | + case 'decrement': return { ...state, count: state.count - state.step } |
| 53 | + case 'reset': return { ...state, count: 0 } |
| 54 | + case 'setStep': return { ...state, step: action.payload } |
| 55 | + default: return state |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// ─── Main Demo Component ────────────────────────────────────────────────────── |
| 60 | +export const HooksDemo: FC = () => { |
| 61 | + // useState |
| 62 | + const [name, setName] = useState('') |
| 63 | + |
| 64 | + // useEffect |
| 65 | + useEffect(() => { |
| 66 | + document.title = name ? `Hello, ${name}!` : 'React Hooks Demo' |
| 67 | + return () => { document.title = 'React Tutorial' } |
| 68 | + }, [name]) |
| 69 | + |
| 70 | + // useReducer |
| 71 | + const [state, dispatch] = useReducer(counterReducer, { count: 0, step: 1 }) |
| 72 | + |
| 73 | + // useRef |
| 74 | + const inputRef = useRef<HTMLInputElement>(null) |
| 75 | + |
| 76 | + // useMemo |
| 77 | + const expensiveValue = useMemo(() => { |
| 78 | + return Array.from({ length: 1000 }, (_, i) => i).reduce((a, b) => a + b, 0) |
| 79 | + }, []) |
| 80 | + |
| 81 | + // useCallback |
| 82 | + const handleReset = useCallback(() => { |
| 83 | + dispatch({ type: 'reset' }) |
| 84 | + setName('') |
| 85 | + inputRef.current?.focus() |
| 86 | + }, []) |
| 87 | + |
| 88 | + return ( |
| 89 | + <ThemeProvider> |
| 90 | + <div> |
| 91 | + <h2>📌 React Hooks</h2> |
| 92 | + |
| 93 | + <section> |
| 94 | + <h3>useState + useEffect</h3> |
| 95 | + <input |
| 96 | + ref={inputRef} |
| 97 | + value={name} |
| 98 | + onChange={e => setName(e.target.value)} |
| 99 | + placeholder="Type your name..." |
| 100 | + style={{ padding: '0.5rem', marginRight: '0.5rem' }} |
| 101 | + /> |
| 102 | + <p>Document title updates via useEffect: <strong>{name || '(empty)'}</strong></p> |
| 103 | + </section> |
| 104 | + |
| 105 | + <section> |
| 106 | + <h3>useReducer</h3> |
| 107 | + <p>Count: <strong>{state.count}</strong> (step: {state.step})</p> |
| 108 | + <button onClick={() => dispatch({ type: 'increment' })}>+{state.step}</button> |
| 109 | + <button onClick={() => dispatch({ type: 'decrement' })} style={{ margin: '0 0.5rem' }}>-{state.step}</button> |
| 110 | + <button onClick={handleReset}>Reset</button> |
| 111 | + <div style={{ marginTop: '0.5rem' }}> |
| 112 | + <label>Step: </label> |
| 113 | + <input |
| 114 | + type="number" |
| 115 | + value={state.step} |
| 116 | + onChange={e => dispatch({ type: 'setStep', payload: Number(e.target.value) })} |
| 117 | + style={{ width: 60, padding: '0.25rem' }} |
| 118 | + /> |
| 119 | + </div> |
| 120 | + </section> |
| 121 | + |
| 122 | + <section> |
| 123 | + <h3>useMemo</h3> |
| 124 | + <p>Sum of 0..999 (memoized): <strong>{expensiveValue}</strong></p> |
| 125 | + </section> |
| 126 | + |
| 127 | + <ThemeToggle /> |
| 128 | + </div> |
| 129 | + </ThemeProvider> |
| 130 | + ) |
| 131 | +} |
| 132 | + |
| 133 | +function ThemeToggle() { |
| 134 | + const { theme, toggleTheme } = useContext(ThemeContext) |
| 135 | + return ( |
| 136 | + <section> |
| 137 | + <h3>useContext</h3> |
| 138 | + <p>Current theme: <strong>{theme}</strong></p> |
| 139 | + <button onClick={toggleTheme}>Toggle Theme</button> |
| 140 | + </section> |
| 141 | + ) |
| 142 | +} |
0 commit comments