DEV Community

Discussion on: useRef or useState, which is better?

 
lexlohr profile image
Alex Lohr

Yes, mutating state both ways is risky, but since I throw away the updater in my example, that risk is exactly 0.0000.

Also, just because it's not documented, it is not necessarily an anti-pattern. There are a lot of nice patterns nowhere to be found in the react documentation. Yes, creating an unneeded function that will be immediately eaten by the garbage collector may not be the best idea, but the point of the example was to show the relation between both hooks in terms even a beginner should be able to understand and not to show a best practice and 3. No again; if you want to warn about problems, show the code that causes the problem inside the given scope. Don't blame me for bad code you've seen elsewhere.

Thread Thread
 
lexlohr profile image
Alex Lohr

You have still failed to show why they would have a headache. Also, I would trust even beginners to be able to see that a useRef method is already available, so they wouldn't have to create their own and therefore recognize that this example was meant to illustrate the difference between useState and useRef and not more.

Thread Thread
 
salehmubashar profile image
Saleh Mubashar

by headache i meant 1) it may be overwhelming to new learners. 2) I am sure I conveyed my point in the post while trying to stay as simple as possible and lastly neither is better, its just about usage

Some comments have been hidden by the post's author - find out more