Skip to content

Instantly share code, notes, and snippets.

@cassidoo
Last active April 23, 2020 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cassidoo/6aa3cd63212682cc4279fd467a92bfb3 to your computer and use it in GitHub Desktop.
Save cassidoo/6aa3cd63212682cc4279fd467a92bfb3 to your computer and use it in GitHub Desktop.
forwardRef example
export const SomeButton = forwardRef(
(
{ children, onSelect, ...props },
forwardedRef
) => {
return (
<button
{...props}
onClick={onSelect}
ref={forwardedRef}
>
{children}
</button>
)
}
)
// And then the user of the component can useRef to make a ref that it can interact with
function SomeButtonUser() {
let buttonRef = useRef()
useLayoutEffect(() => {
const { current } = buttonRef
// do stuff with the ref
}, [])
return (
<div>
I have a button here:
<SomeButton ref={buttonRef} onClick={() => { console.log("I was clicked!") }}>poop</SomeButton>
</div>)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment