[英]Trouble using a React component from an older version of React in v16.x
[英]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>
);
也許這可以工作?
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.