Built-in Hooks
React provides several built-in Hooks for different purposes:State Hooks
State lets a component remember information like user input.useState- Declares a state variableuseReducer- Declares a state variable with reducer logic
Context Hooks
Context lets a component receive information from distant parents without passing it as props.useContext- Reads and subscribes to a context
Ref Hooks
Refs let a component hold information that isn’t used for rendering, like a DOM node.useRef- Declares a refuseImperativeHandle- Customizes the ref exposed by your component
Effect Hooks
Effects let a component connect to and synchronize with external systems.useEffect- Connects to an external systemuseLayoutEffect- Fires before browser paintuseInsertionEffect- Fires before React makes DOM changes
Performance Hooks
These hooks help optimize re-rendering performance.useMemo- Caches the result of an expensive calculationuseCallback- Caches a function definitionuseTransition- Marks state updates as non-urgentuseDeferredValue- Defers updating a part of the UI
Resource Hooks
These hooks let components access resources without them being part of their state.use- Reads the value of a Promise or Context
Other Hooks
useId- Generates unique IDsuseSyncExternalStore- Subscribes to an external storeuseActionState- Manages state for Server ActionsuseOptimistic- Shows optimistic UI updatesuseDebugValue- Customizes debug display in React DevTools
Rules of Hooks
Hooks are JavaScript functions with two important rules:Only call Hooks at the top level
Don’t call Hooks inside loops, conditions, or nested functions. Instead, always use Hooks at the top level of your React function, before any early returns.Only call Hooks from React functions
Don’t call Hooks from regular JavaScript functions. Instead, you can:- ✅ Call Hooks from React function components
- ✅ Call Hooks from custom Hooks
Custom Hooks
You can create your own Hooks to extract component logic into reusable functions. Custom Hooks are JavaScript functions whose name starts with “use” and that may call other Hooks.Custom Hooks let you share stateful logic, not state itself. Each call to a Hook is completely independent from every other call to the same Hook.
Hook Call Requirements
TypeScript Support
All React Hooks have built-in TypeScript types. You can use generics to specify types:Performance Considerations
- Hooks don’t add runtime overhead compared to class components
- State updates are batched automatically in React 18+
- Use performance hooks like
useMemoanduseCallbackonly when needed - Don’t optimize prematurely - measure first
Migration from Classes
If you’re migrating from class components:| Class Component | Hook Equivalent |
|---|---|
this.state | useState |
this.setState | useState setter |
componentDidMount | useEffect(() => {...}, []) |
componentDidUpdate | useEffect(() => {...}) |
componentWillUnmount | useEffect cleanup function |
this.myRef | useRef |
| Context Consumer | useContext |
Next Steps
- Start with
useStatefor basic state management - Learn
useEffectfor side effects - Explore
useContextfor prop drilling solutions - Dive into performance hooks when needed