Hooks are much easier than class components were for 80% of use cases, but the remaining 20% can be a bit hairy if you aren't so familiar with how the underlying system works. I ran into this exact kind of issue when doing mouse move events recently because I was using addEventListener in the component and getting stale state references.
Ultimately useRef is a good way to solve the problem.
I ran into this exact issue the other day while trying to get mouse move events from the document object. I wanted to increment a counter in my React state based on the mouse moves, but I kept getting the same old state in my event listener, which meant I was just incrementing on the same old value and got stuck.
I did use useRef to store a copy of the state, but it seemed like a very hacky workaround, having to duplicate the state essentially.
Is there a better way to use useRef than the way I am doing it? I really just want to use mouse move events on document to implement draggable elements.
Thanks! So I did a test using the updater function. Basically I am making a little dial knob component (similar to what you see in digital audio workstation software). The knob has a value and you can change that value by clicking and dragging on it.
The problem with using the updater function is that I can only access the latest state within the function context, which means I can't do things like call a callback prop that passes the knob value back to the parent because React will complain that I am attempting to re-render a parent and its child at the same time.
Now the argument can be made that I should make the child component be stateless, but then that means that whatever callback prop the parent passes into the knob has to accept an updater function. This means that some aspects of the knob implementation is leaking into the parent, which is less than ideal.
I would make it so that knob just calles onChange function and passes in direction and amount that it moved and then its parents job to update the state. And now there is no leaking of responsibility.
Passing just the delta back out of the component does "fix" the leak, but it also makes the component's interface less than ideal because when you think about it, what do you really want from a knob? Just the value that its currently set to, not the delta of the value from the previous value. You're sorta making the parent do more of the lifting than it really should and if all consumers of this knob component now have to worry about adding this delta to whatever they think is the state, it gets a bit cumbersome over time, especially if this component is used a lot.
I wish there was some react-compatible way to register DOM EventListeners. Like maybe a React.document.onmouseover or something. I don't know, maybe not possible, but still, it would make custom components a heck of a lot easier to do if you could.
Thanks anyways for all the suggestions! I've always wondered when I would ever use state updater functions and now I know.
11
u/Exoskele Mar 06 '23 edited Mar 07 '23
Hooks are much easier than class components were for 80% of use cases, but the remaining 20% can be a bit hairy if you aren't so familiar with how the underlying system works. I ran into this exact kind of issue when doing mouse move events recently because I was using addEventListener in the component and getting stale state references.
Ultimately useRef is a good way to solve the problem.