You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+16Lines changed: 16 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -7285,6 +7285,22 @@ Technically it is possible to write nested function components but it is not sug
7285
7285
7286
7286
**[⬆ Back to Top](#table-of-contents)**
7287
7287
7288
+
305. ### What is `useCallback` and why is it used?
7289
+
7290
+
The `useCallback` is a React Hook used to memoize **function definitions** between renders. It returns the same function reference unless its dependencies change. This is especially useful when passing callbacks to optimized child components (e.g. those wrapped in `React.memo`) to prevent unnecessary re-renders.
7291
+
7292
+
**Example:**
7293
+
7294
+
```css
7295
+
consthandleClick=useCallback(() => {
7296
+
console.log('Button clicked');
7297
+
}, []);
7298
+
```
7299
+
7300
+
Without `useCallback`, a new function is created on every render, potentially causing child components to re-render unnecessarily.
7301
+
7302
+
**[⬆ Back to Top](#table-of-contents)**
7303
+
7288
7304
## Old Q&A
7289
7305
7290
7306
1. ### Why should we not update the state directly?
0 commit comments