2

I'm managing my forms using react-hook-form. but I also need to manage an element's click event. In this case, I have to set both refs

   let inputRef = useRef(null);

   const onClick = input => {
      input.click();
   };

<input
     type="file" 
     ref={ref => {
        inputRef = ref;
        register();
     }},
     onClick={() => onClick(inputRef)}
></input>

How can I set multiple refs

pneuma
  • 241
  • 5
  • 14

3 Answers3

1
              inputRef={ref => {
                 inputLogoSquare = ref;
                 register(ref);
              }}

this is the answer

pneuma
  • 241
  • 5
  • 14
0

I think it's the best solution.

const myRef = useRef(null);

<input ref={(ref) => { 
         myRef.current = ref; 
         register(ref); 
       }} />
iranimij
  • 91
  • 10
0
const myRef = useRef<any>();
const { ref, ...rest } = register("email");
<input
   ref={(e) => {
     ref(e);
     myRef.current = e;
   }}
   {...rest}
/>
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Sep 04 '22 at 13:31