React Cheatsheet
React 18/19 hooks, component patterns, state management, and best practices for modern apps.
React is the most popular UI library for building interactive interfaces. This cheatsheet focuses on hooks (the modern way to write React), component composition, data fetching patterns, and performance optimization.
All examples use React 18+ with TypeScript and functional components.
Hooks — State
const [state, setState] = useState(initial)const [val, setVal] = useState(() => expensive())Hooks — Effects & Refs
useEffect(() => { ... }, [deps])useEffect(() => { ... }, [])const ref = useRef(initial)Hooks — Performance
const memoized = useMemo(() => compute(a, b), [a, b])const fn = useCallback(() => { ... }, [deps])React.memo(Component)Hooks — Context & Reducers
const ctx = createContext(default)const val = useContext(MyContext)const [state, dispatch] = useReducer(reducer, init)Component Patterns
<Child onClick={handleClick}>{children}const ctx = createContext<Type | null>(null)Forms & Events
<input value={val} onChange={e => setVal(e.target.value)} /><form onSubmit={handleSubmit}>React Cheatsheet
React 18/19 hooks, component patterns, state management, and best practices for modern apps.
React is the most popular UI library for building interactive interfaces. This cheatsheet focuses on hooks (the modern way to write React), component composition, data fetching patterns, and performance optimization.
All examples use React 18+ with TypeScript and functional components.
Hooks — State
const [state, setState] = useState(initial) — Declare a state variable with its setter. Re-renders on update.const [val, setVal] = useState(() => expensive()) — Lazy initialization — pass a function that runs only on first render.Hooks — Effects & Refs
useEffect(() => { ... }, [deps]) — Run side effects after render. Return a cleanup function.useEffect(() => { ... }, []) — Empty deps — runs once after initial mount (like componentDidMount).const ref = useRef(initial) — Persist a mutable value across renders. Changing it does not re-render.Hooks — Performance
const memoized = useMemo(() => compute(a, b), [a, b]) — Memoize expensive computations. Recalculates only when deps change.const fn = useCallback(() => { ... }, [deps]) — Memoize a callback so it does not change unless deps change.React.memo(Component) — Prevent re-render when props are shallowly equal.Hooks — Context & Reducers
const ctx = createContext(default) — Create a context for passing data through the component tree without prop drilling.const val = useContext(MyContext) — Consume the nearest context value above in the tree.const [state, dispatch] = useReducer(reducer, init) — For complex state logic. Dispatch actions to a reducer function.Component Patterns
<Child onClick={handleClick}> — Pass data down and events up. Never mutate props.{children} — Compose components using the children prop for flexible layouts.const ctx = createContext<Type | null>(null) — Define a strongly typed context with TypeScript.Forms & Events
<input value={val} onChange={e => setVal(e.target.value)} /> — Controlled component — React controls the input value.<form onSubmit={handleSubmit}> — Handle form submission. Prevent default to avoid page reload.