Catalog / React Hooks Cheatsheet
React Hooks Cheatsheet
A quick reference for React Hooks, covering basic usage, rules, and common hooks with examples.
Basic Hooks
useState
Description: |
Manages state within a functional component. |
Usage: |
|
Initial State: |
Can be a value or a function that returns the initial value (lazy initialization). |
setState: |
A function to update the state. Triggers a re-render. |
Example: |
|
useEffect
Description: |
Performs side effects in functional components (data fetching, subscriptions, etc.). |
Usage: |
|
Dependencies: |
An array of values. Effect runs only if these values change. |
Cleanup: |
Optional function to clean up resources (e.g., unsubscribe from a subscription) when the component unmounts or before the effect runs again. |
Example: |
|
useContext
Description: |
Consumes a context value. Allows accessing data from a provider higher up in the component tree. |
Usage: |
|
Context: |
Must be created using |
Provider: |
A component that provides the context value to its children. |
Example: |
|
Additional Hooks
useRef
Description: |
Creates a mutable ref object whose |
Usage: |
|
Accessing Value: |
Access the current value via |
Example: |
|
useReducer
Description: |
An alternative to |
Usage: |
|
Reducer: |
A function that takes the current state and an action, and returns the new state. |
Dispatch: |
A function to dispatch actions to the reducer. |
Example: |
|
useMemo
Description: |
Memoizes a value. Only recomputes the value when one of the dependencies has changed. Improves performance by avoiding expensive calculations on every render. |
Usage: |
|
Dependencies: |
An array of dependencies. The function will only re-run when one of the dependencies changes. |
Example: |
|
Callback and Layout Hooks
useCallback
Description: |
Memoizes a callback function. Returns a memoized version of the callback that only changes if one of the dependencies has changed. Useful when passing callbacks to optimized child components that rely on reference equality to prevent unnecessary renders. |
Usage: |
|
Dependencies: |
An array of dependencies. The callback will only be recreated when one of the dependencies changes. |
Example: |
|
useLayoutEffect
Description: |
Identical to |
Usage: |
|
When to Use: |
Use when you need to perform DOM measurements or mutations before the browser paints the screen to avoid visual glitches. Example: measuring the size of an element and adjusting its position. |
Example: |
|
Rules of Hooks
Key Principles
Only Call Hooks at the Top Level Do not call Hooks inside loops, conditions, or nested functions. Ensure that Hooks are always called in the same order each time a component renders. |
Only Call Hooks from React Functions Call Hooks from React function components or custom Hooks. Do not call Hooks from regular JavaScript functions. |
Linting Rules
The
Then configure ESLint to use the plugin. |
Custom Hooks
Description: |
Custom Hooks allow you to extract component logic into reusable functions. A custom Hook is a JavaScript function whose name starts with “use” and that may call other Hooks. |
Example: |
|