Skip to content

How to bind custom method or property with component's ref? #564

Answered by ryansolid
Sioxas asked this question in Q&A
Discussion options

You must be logged in to vote

Oh sure. We don't have a hook for it. All you need to do is call props.ref as a function. The only caveat is it may not exist, which could be hidden behind a hook of sorts I suppose but you can also just check that it exists.

Converting the example from the linked docs I think it's just this?

function FancyInput(props) {
  const inputRef = (el) => {
    props.ref?.({
      focus() { el.focus(); }
    })
  }
  return <input ref={inputRef} ... />;
}

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@dolsem
Comment options

@teddybee
Comment options

@rburgst
Comment options

@andrewgreenh
Comment options

Answer selected by ryansolid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants
Converted from issue

This discussion was converted from issue #563 on July 15, 2021 18:31.