|
46 | 46 | - [Cleanup Functions (optional)](#-cleanup-functions-optional)
|
47 | 47 | 7. [useReducer](#-usereducer)
|
48 | 48 | 8. [useRef](#-useref)
|
| 49 | +9. [useCallback](#-usecallback) |
49 | 50 |
|
50 | 51 | <br>
|
51 | 52 |
|
@@ -1162,3 +1163,75 @@ Use Cases:
|
1162 | 1163 | > ```
|
1163 | 1164 | - Storing Mutable Data:
|
1164 | 1165 | > You can use `useRef` to store data that should not trigger a re-render when it changes. This is useful when keeping track of values between renders, such as the previous state of a component.
|
| 1166 | +
|
| 1167 | +<p align="right"> |
| 1168 | + <a href="#reactjs">back to top ⬆</a> |
| 1169 | +</p> |
| 1170 | +
|
| 1171 | +<br> |
| 1172 | +<br> |
| 1173 | +
|
| 1174 | +## 🔶 useCallback |
| 1175 | +
|
| 1176 | +In React, `useCallback` is a hook used to memoize functions, especially callback functions, to prevent unnecessary re-renders of child components. |
| 1177 | +
|
| 1178 | +When you create a function inside a functional component, it gets recreated on each render. |
| 1179 | +
|
| 1180 | +`useCallback` helps by memoizing the function, ensuring it remains the same between renders unless its dependencies change. |
| 1181 | +
|
| 1182 | +Syntax: |
| 1183 | +
|
| 1184 | +> ```jsx |
| 1185 | +> const memoizedCallback = useCallback(callback, dependenciesArray); |
| 1186 | +> ``` |
| 1187 | +> |
| 1188 | +> `useCallback` returns a memoized version of the callback function you provide, but it doesn't execute the callback function itself. |
| 1189 | +
|
| 1190 | +How it works: |
| 1191 | +
|
| 1192 | +- First render: |
| 1193 | + > During the first render, it creates and returns the memoized callback function provided as the first argument. The callback doesn't run at this stage. |
| 1194 | +- Next renders: |
| 1195 | + - When the second argument is an empty array: |
| 1196 | + > Returns the same memoized original callback from early renders. This is useful when you want to ensure the callback function remains the same, effectively preventing it from being recreated. |
| 1197 | + - When the second argument contains elements that have changed since the last render: |
| 1198 | + > Recreates the callback function and returns the new version. |
| 1199 | +
|
| 1200 | +Example: |
| 1201 | +
|
| 1202 | +```jsx |
| 1203 | +import { useState, useCallback } from "react"; |
| 1204 | +
|
| 1205 | +const MyComponent = () => { |
| 1206 | + const [count, setCount] = useState(0); |
| 1207 | +
|
| 1208 | + // Without useCallback |
| 1209 | + const handleClickWithoutCallback = () => { |
| 1210 | + console.log("Button clicked!", count); |
| 1211 | + }; |
| 1212 | +
|
| 1213 | + // With useCallback |
| 1214 | + const handleClickWithCallback = useCallback(() => { |
| 1215 | + console.log("Button clicked!", count); |
| 1216 | + }, [count]); |
| 1217 | +
|
| 1218 | + return ( |
| 1219 | + <div className="text-white"> |
| 1220 | + <button onClick={handleClickWithoutCallback}> |
| 1221 | + Click without useCallback |
| 1222 | + </button> |
| 1223 | + <button onClick={handleClickWithCallback}>Click with useCallback</button> |
| 1224 | + <p>Count: {count}</p> |
| 1225 | + <button |
| 1226 | + onClick={() => { |
| 1227 | + setCount((curCount) => curCount + 1); |
| 1228 | + }} |
| 1229 | + > |
| 1230 | + Click to increment count! |
| 1231 | + </button> |
| 1232 | + </div> |
| 1233 | + ); |
| 1234 | +}; |
| 1235 | +``` |
| 1236 | +
|
| 1237 | +> `useCallback` is generally more beneficial when dealing with complex components or when optimizing performance becomes crucial. |
0 commit comments