Languages
[Edit]
EN

React - use current callback

5 points
Created by:
Lani-Skinner
748

In this short article we would like to show how in React use always current reference to some callback.

As current callback reference we understand: using same function between component rendeinrg cycles. The solution is to use useRef hook to store function reference.

Note: read this article to see how to create custom useCurrentCallback hook.

Practical solution:

Component without actual function reference

Edit

This section presents case what if we could do not use useRef to store function reference.

The issue of below example is: the counter is not incremented in right way, because calback reference used inside setInterval(...) indicates function from first rendering cycle.

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - use current callback
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join