Consider I have a custom hook that uses one or more refs to DOM elements, I see two distinct ways how to write/use a hook like that (and I know there are probably even more nuances – e. g. callback refs).
Option 1: return a ref from custom hook and use it in a component:
const Option1 = () => {
const hooksRef = myCustomHookReturninARef()
return <div ref={hooksRef}>Using returned ref as prop.</div>
}
Option 2: create a ref in a component and pass it to a custom hook:
const Option2 = () => {
const componentsRef = React.createRef()
myCustomHookExpectingRefArgument(componentsRef)
return <div ref={componentsRef}>Using created ref as prop..</div>
}
I've been using either options and they both seems to be working fine. I know this is likely an opinionated question, but:
Are there some significant drawbacks using the first vs the second approach?