datavisyn / visyn_core

BSD 3-Clause "New" or "Revised" License
1 stars 1 forks source link

Added setRef hook #395

Closed dvmoritzschoefl closed 1 month ago

dvmoritzschoefl commented 1 month ago

The problem: Suppose we have an element which we track with useRef

const ref = React.useRef<HTMLElement>();
<div ref={ref} />

The ref will not trigger useEffects since the reference never changes, this can cause stale state as seen in pharmDB with the wheel listeners.

The solution: use a ref callback, which will trigger everytime the component remounts. I encapsulated this logic into a reusable form

For reference, see: https://legacy.reactjs.org/docs/hooks-faq.html?source=post_page-----eb7c15198780--------------------------------#how-can-i-measure-a-dom-node https://medium.com/@teh_builder/ref-objects-inside-useeffect-hooks-eb7c15198780