|
| 1 | +## 1. useState: The Memory |
| 2 | + |
| 3 | +**What it does:** Allows a functional component to "remember" information between renders. |
| 4 | + |
| 5 | +**When to use:** Whenever you have data that changes over time and needs to trigger a re-render to update the UI (e.g., form inputs, toggle states, counters). |
| 6 | + |
| 7 | +```javascript |
| 8 | +const [count, setCount] = useState(0); |
| 9 | + |
| 10 | +// Update state |
| 11 | +setCount(count + 1); |
| 12 | +``` |
| 13 | + |
| 14 | +## 2. useEffect: The Side Effect |
| 15 | + |
| 16 | +**What it does:** Performs side effects in functional components. "Side effects" are things like data fetching, subscriptions, or manually changing the DOM. |
| 17 | + |
| 18 | +**When to use:** When you need to do something *after* the component renders or when a specific value changes. |
| 19 | + |
| 20 | +```javascript |
| 21 | +useEffect(() => { |
| 22 | + // This runs after every render |
| 23 | + document.title = `You clicked ${count} times`; |
| 24 | + |
| 25 | + // Optional cleanup mechanism |
| 26 | + return () => { |
| 27 | + // Clean up code here |
| 28 | + }; |
| 29 | +}, [count]); // Only re-run if 'count' changes |
| 30 | +``` |
| 31 | + |
| 32 | +## 3. useMemo: The Calculator |
| 33 | + |
| 34 | +**What it does:** Memoizes (caches) the *result* of a calculation. It only re-calculates the value when one of its dependencies changes. |
| 35 | + |
| 36 | +**When to use:** Optimization. Use it to avoid expensive calculations on every render. |
| 37 | + |
| 38 | +```javascript |
| 39 | +const expensiveValue = useMemo(() => { |
| 40 | + return computeExpensiveValue(a, b); |
| 41 | +}, [a, b]); // Only re-compute if 'a' or 'b' changes |
| 42 | +``` |
| 43 | + |
| 44 | +*Note: Don't overuse this. Memoization has its own cost.* |
| 45 | + |
| 46 | +## 4. useCallback: The Function Saver |
| 47 | + |
| 48 | +**What it does:** Memoizes a *function definition*. It returns the same function instance between renders unless its dependencies change. |
| 49 | + |
| 50 | +**When to use:** Optimization. Primarily useful when passing callbacks to optimized child components (like those wrapped in `React.memo`) to prevent unnecessary re-renders of the child. |
| 51 | + |
| 52 | +```javascript |
| 53 | +const handleClick = useCallback(() => { |
| 54 | + doSomething(a, b); |
| 55 | +}, [a, b]); // Function identity remains stable unless 'a' or 'b' changes |
| 56 | +``` |
| 57 | + |
| 58 | +## Summary Table |
| 59 | + |
| 60 | +| Hook | Returns | Purpose | Re-runs when... | |
| 61 | +| :--- | :--- | :--- | :--- | |
| 62 | +| **useState** | `[state, setter]` | Manage state | Setter is called | |
| 63 | +| **useEffect** | `undefined` | Side effects | Dependencies change | |
| 64 | +| **useMemo** | Calculated Value | Cache expensive calculation | Dependencies change | |
| 65 | +| **useCallback** | Memoized Function | Stable function identity | Dependencies change | |
| 66 | + |
| 67 | +## Key Difference: useMemo vs useCallback |
| 68 | + |
| 69 | +* `useMemo` caches the **result** of a function call. |
| 70 | +* `useCallback` caches the **function itself**. |
| 71 | + |
| 72 | +> `useCallback(fn, deps)` is equivalent to `useMemo(() => fn, deps)`. |
0 commit comments