Skip to content

Commit 6bd02b2

Browse files
committed
Add useCallback question
1 parent da5c520 commit 6bd02b2

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7285,6 +7285,22 @@ Technically it is possible to write nested function components but it is not sug
72857285
72867286
**[⬆ Back to Top](#table-of-contents)**
72877287
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+
const handleClick = 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+
72887304
## Old Q&A
72897305
72907306
1. ### Why should we not update the state directly?

0 commit comments

Comments
 (0)