React Cheatsheet - Hooks & Components
This reference is for React developers who write function components and Hooks. It moves from the state/effect core to advanced hooks such as useReducer, context, and useId, then component patterns like forwardRef and render props, before controlled forms, state via Context, and performance optimizations like React.memo and lazy. Each entry is a copy-ready snippet with the deps array and edge cases called out. After reading you should be able to drive async data with useEffect, share state across components with Context, and stop wasted re-renders with memo/useCallback.
Core Hooks 6
const [count, setCount] = useState(0)const [user, setUser] = useState({ name: "", age: 0 })useEffect(() => { fetchData() }, [])useEffect(() => { subscribe(id); return () => unsubscribe() }, [id])const ref = useRef(null)const memoized = useMemo(() => compute(a, b), [a, b])Advanced Hooks 6
const [state, dispatch] = useReducer(reducer, initialState)const value = useContext(MyContext)useLayoutEffect(() => { measure() }, [deps])useImperativeHandle(ref, () => ({ focus: () => {} }))const id = useId()const [isPending, startTransition] = useTransition()Component Patterns 6
function App({ name, children }) { return <div>{children}</div> }const Button = forwardRef((props, ref) => <button ref={ref} />)function withAuth(Component) { return (props) => <Component {...props} /> }function List({ render }) { return items.map(render) }const Memo = React.memo(Component)const Lazy = React.lazy(() => import("./Comp"))Events & Forms 5
<button onClick={(e) => handleClick(e)}>Click</button><input value={val} onChange={e => setVal(e.target.value)} /><form onSubmit={e => { e.preventDefault(); submit() }}>e.stopPropagation()e.preventDefault()State Management 5
setCount(prev => prev + 1)setUser(prev => ({ ...prev, name: "new" }))const ThemeContext = createContext("light")<ThemeContext.Provider value="dark">const value = useContext(ThemeContext)Performance 5
const MemoComp = React.memo(MyComponent)const value = useMemo(() => expensive(a, b), [a, b])const handler = useCallback(() => {}, [deps])<Suspense fallback={<Loading />}><Lazy /></Suspense>const [deferred] = useDeferredValue(value)Common Patterns 6
{isLoading ? <Loading /> : <Content />}{show && <Menu />}{items.map(item => <li key={item.id}>{item.name}</li>)}class ErrorBoundary extends React.Component { static getDerivedStateFromError(e) {} }ReactDOM.createPortal(<Modal />, document.body)<Comp {...defaultProps} {...props} />Tips
- An empty [] deps array runs the effect once on mount, like componentDidMount.
- useMemo/useCallback optimize by avoiding needless recompute and re-render.
- React 18 StrictMode double-invokes effects in dev to surface side-effect bugs.
- Suspense + React.lazy enables lazy loading and code splitting.
- Always give list items a stable key; avoid using array index.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 21, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us