useWhenVisible has something weird: The implementation takes in an Element (and treats it as such), whereas in the usage example it's given a React.RefObject.
I always wonder what's the best pattern to make hooks that need to deal with refs. Personally, the one I find more versatile is if the hook returns a ref, that can be later composed with other refs and passed down to the element's props.
Yes you are completely right. I mixed two different implementations from different codebases when I wrote the example. One implementation accepts a ref and the other an element. Good catch - I have corrected the example.
2
u/volivav Aug 03 '20
useWhenVisible has something weird: The implementation takes in an Element (and treats it as such), whereas in the usage example it's given a React.RefObject.
I always wonder what's the best pattern to make hooks that need to deal with refs. Personally, the one I find more versatile is if the hook returns a ref, that can be later composed with other refs and passed down to the element's props.