简体   繁体   English

如何反应分配多个参考react-hook-form

[英]how to react assign multiple refs react-hook-form

I'm managing my forms using react-hook-form.我正在使用 react-hook-form 管理我的 forms。 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}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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