简体   繁体   中英

how to react assign multiple refs react-hook-form

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

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

this is the answer

I think it's the best solution.

const myRef = useRef(null);

<input ref={(ref) => { 
         myRef.current = ref; 
         register(ref); 
       }} />
const myRef = useRef<any>();
const { ref, ...rest } = register("email");
<input
   ref={(e) => {
     ref(e);
     myRef.current = e;
   }}
   {...rest}
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM