Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[React 19], useCallback not needed now!!! #31913

Closed
TonyFTannous-Byd opened this issue Dec 26, 2024 · 4 comments
Closed

[React 19], useCallback not needed now!!! #31913

TonyFTannous-Byd opened this issue Dec 26, 2024 · 4 comments
Labels

Comments

@TonyFTannous-Byd
Copy link

TonyFTannous-Byd commented Dec 26, 2024

my question is, in react 19, the usage of useCallback is deprecated, means react automatically memoize the functions in my functional component? or only memoize the function that hasn't any dependencies. please more clarification about this.
if it is not required, and my app already i used useCallback, and memo... i have any side effect if i continue used it or i need to removed from my app if i want to switch from 18 to 19

thanks.

@HAWKZ4
Copy link

HAWKZ4 commented Dec 26, 2024

Hi @TonyFTannous-Byd

  1. useCallback is not deprecated in React 19, but React's rendering improvements may reduce its necessity in some cases

  2. React does not automatically memoize functions. You still need useCallback to explicitly memoize functions, especially those with dependencies

  3. Keeping useCallback when upgrading to React 19 has no harmful side effects, but it might add unnecessary complexity if
    overused

  4. Remove useCallback only where it doesn’t provide meaningful performance benefits, especially in non-performance-critical parts of your app

  5. Use tools like React DevTools to profile your app. Keep useCallback where it helps, and simplify your code where it doesn’t

Recommendation: Gradually audit your use of useCallback :

  • keep it where necessary and remove it elsewhere to simplify your code

@TonyFTannous-Byd
Copy link
Author

Hi @HAWKZ4 , thanks for your answere
if you use React 19 only, you are right but using with it new react compiler (npm install -D eslint-plugin-react-compiler), no need for useCallback memo ....
this what react 19 documentation told (https://react.dev/learn/react-compiler)

@josephsavona
Copy link
Contributor

As @HAWKZ4 noted, useCallback is not deprecated in React 19 (you can find the deprecations and some upcoming anticipated deprecations mentioned in https://react.dev/blog/2024/12/05/react-19). You should continue using useCallback in 19 as you did in previous versions, unless you're enabling the compiler. Note that the compiler is still in beta, so for performance-critical areas you may still want to use useMemo and useCallback (the compiler will skip your code if it can't memoize at least as much as you did manually).

@TonyFTannous-Byd
Copy link
Author

@josephsavona As a summary, I will continue using useCallback and useMemo as before. The compiler will manage them, meaning that if it detects they are unnecessary, it will remove them from the output bundle. Otherwise, it will keep them as-is or optimize them further if it determines they can be written in a more useful way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants