3

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

3
  • 1
    will it help? - stackoverflow.com/questions/54633690/… Commented Apr 24, 2021 at 22:49
  • 1
    Thank you for trying to help. I found the answer Commented Apr 24, 2021 at 22:51
  • 1
    ok, happy coding to you Commented Apr 24, 2021 at 22:53

3 Answers 3

2
const myRef = useRef<any>();
const { ref, ...rest } = register("email");
<input
   ref={(e) => {
     ref(e);
     myRef.current = e;
   }}
   {...rest}
/>
Sign up to request clarification or add additional context in comments.

1 Comment

While this code may solve the question, including an explanation 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.
1
              inputRef={ref => {
                 inputLogoSquare = ref;
                 register(ref);
              }}

this is the answer

1 Comment

It's not working for me, could you please explain more?
0

I think it's the best solution.

const myRef = useRef(null);

<input ref={(ref) => { 
         myRef.current = ref; 
         register(ref); 
       }} />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.