簡體   English   中英

將 useRef 與舊版本的反應組件一起使用?

[英]using useRef with older version react component?

如何將 useRef 用於使用舊 ref 回調 api 的組件?

const App = () => (
  //how to get ref of input?
  <div>
    <Input ref={r => console.log(r)} />
    <Button type="primary">Hello</Button>
  </div>
);

https://codesandbox.io/s/hello-antd-p6peq

也許這可以工作?

const App = () => {
  const myRef = useRef(null);

  return (
    <div>
      <Input
        ref={r => {
          myRef.current = r;
        }}
      />
      <Button type="primary">Hello</Button>
    </div>
  );
};

你不能有一個useRef ??? 這是一個功能組件

const App = () => {
  const inputRef = useRef(null);

  // console.log(inputRef.current) //  => <Input />
  return (
    <div>
      <Input ref={inputRef} />
      <Button type="primary">Hello</Button>
    </div>
  );
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM