A
A
Alexander2021-08-03 00:30:25
React
Alexander, 2021-08-03 00:30:25

How to pass your ref to react-hook-form?

How to pass your ref to react-hook-form?
Initially, I had my own ref for input, but when I turned on react-hook-form, I ran into the following problem
How to combine two refs in one input, and also pass my callback to onBlur?
I will be grateful for the answer

const Field = React.forwardRef<any, IField>(({ name, onChange, onBlur, value, type = 'text', label, icon }, ref) => {
  const [isActive, setActive] = React.useState<boolean>(false)
  console.log(ref.length)

  const inputRef = React.useRef<HTMLInputElement>(null)

  const onBlurHandler = () => {
    if (inputRef.current && inputRef.current.value.trim() != '') {
      setActive(true)
    } else {
      setActive(false)
    }
  }

  return (
    <FieldBox>
      <Input ref={ref} isActive={isActive} onBlur={onBlurHandler} type='text' name='telegram' id='telegram' />
      <Label>
        {icon}
        {label}
      </Label>
      <Error />
    </FieldBox>
  )
})

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question