Skip to content

Instantly share code, notes, and snippets.

@thebuilder thebuilder/hookWithRef.js
Last active Feb 11, 2019

Embed
What would you like to do?
Hook with ref callback
import React, {useState} from 'react'
function useHookWithRefCallback() {
const [node, setRef] = useState(null)
useEffect(
() => {
if (node) {
// Your Hook now has a reference to the ref element.
}
},
[node],
)
return [setRef]
}
function Component() {
// In your component you'll still recieve a `ref`, but it
// will be a callback function instead of a Ref Object
const [ref] = useHookWithRefCallback()
return <div ref={ref}>Ref element</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.