Discuss useRef
usage
#1143
Replies: 7 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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
useRef can be used :
State management We can already cover 100% of any use case with a state management library and React state management. Even setTimeout ids, even if it may not be as localized and easy to do. useRef is very low level compared to a state management library, this can lead to unwanted side effects :
While I understand the need for a more "flexible" way to play with state and re-renders in React, I think it's always a tradeoff, and what you gain in flexibility and "freedom" you lose in maintainability and scalability of the codebase. People of all skill levels need to be able to contribute, relying on a robust and known state management library is essential to help everyone contributing. They can look for answers on internet for problems that are already shared with the state management library's community. While we don't have a standard state management library as of 2023 for React, state management is and will remain a core part of frontend development. Going back to a low-level way to handle it because we don't have a standard trend to follow can be understandable but will actually cause more problems than using a non "approved" library. Recoil is using useRef under the hood, and they abstract it for us behind an API. We are often surprised to see how libraries does their magic under the hood and it's not always an example to follow for a business application codebase. We can cover all the use cases where we need to be "out" of React's loop to avoid re-renders with We can also use React's contexts for const values, to avoid initializing Recoil atoms that are just read only state. Re-render management
Mutable values It's not clear here why we would need to store values out of React. We can certainly do it through many ways :
useRef feels a bit like a static property of a class, but we've seen that class components and too much OOP doesn't fit well with React and frontend development in general. All those ways share the same inherent problem : manual synchronization with React and the render loop, which leads to state management, see above. Conclusion While useRef can certainly be used for doing what can already be done with more appropriate and known patterns, there's no clear reason here why we would need to use it except to "try" a new thing, which can be exciting and would be indicated in a state management library codebase, but not for a business application codebase like Twenty. Although we shouldn't need any useRef at all except for talking with HTML, we should be tolerant with useRefs that are currently used but let's keep in mind that they could be removed, for the reasons described above. State management is one of the most tedious topics in front end development. Let's keep it simple by following clear and opinionated patterns that are already shared with the community so that everybody can contribute easily. |
Beta Was this translation helpful? Give feedback.
-
Closing as there hasn't been any activity recently so the topic seems to be settled! |
Beta Was this translation helpful? Give feedback.
-
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.
Beta Was this translation helpful? Give feedback.
All reactions