-
Notifications
You must be signed in to change notification settings - Fork 2.4k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Discuss useRef
usage
#1009
Comments
I completely agree. While The major advantage here is that changes to these references do not trigger component re-renders, which can significantly improve performance in scenarios where the reference is updated frequently but the view does not need to be refreshed. Beyond this, I think In essence, we can look at |
pHappy to discuss it,
As this is introducing a second way to do things we can do with setState with the same complexity (TBH, I personally think that useRef API actually complexifies code reading but this is subjective), I would strongly advocate to have one way of doing things. This will help keeping the codebase uniform, and prevent discussions on either we should use useRef or useState in "THIS" specific cases (which will likely never ends are they are similar). Keeping clear coding conventions is an important winning condition on this project. In a nutshell, I would really push to keep only using useState (+ useCallback) on the project, except if there is no easy way to do so. Do you have a particular example into mind where you would really need useRef or it it significantly more convenient? @lucasbordeau has refactored the whole table component (which is by far the most complex we have), there are no more re-render on table, it's performant and no useRef have been used |
Appreciate your perspective on this @charlesBochet. While I understand your concern for keeping the codebase clean and streamlined, I believe that
A practical example of using As for the refactor by @lucasbordeau, it's a really great job! However, the shared state management in that case involves different considerations and might not directly apply to all situations where we could potentially benefit from In conclusion, for me, while |
From the react documentation, community threads and various articles, I tend to agree we should restrain its usage to:
On storing the result of complex calculations, I tend to agree with @charlesBochet 's point to have it in a separate component as it prevents re-renders and brings code separation without the inconvenience of Also, on the point that |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
https://react.dev/reference/react/useRef
As discussed with @magrinj and @emilienchvt, we would like to reassess how we can use this hook for better performance. Right now it is mostly only used for manipulating the DOM with HTML element refs but it could be useful in other cases where we want to keep reference to a value without triggering re-renders.
The text was updated successfully, but these errors were encountered: