我有以下内容(使用 Material UI)....
import React from "react";
import { NavLink } from "react-router-dom";
import Tabs from "@material-ui/core/Tabs";
import Tab from "@material-ui/core/Tab";
function LinkTab(link){
return <Tab component={NavLink}
to={link.link}
label={link.label}
value={link.link}
key={link.link}
/>;
}
在新版本中,这会导致以下警告...
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of
ForwardRef
. in NavLink (created by ForwardRef)
我尝试更改为...
function LinkTab(link){
// See https://material-ui.com/guides/composition/#caveat-with-refs
const MyLink = React.forwardRef((props, ref) => <NavLink {...props} ref={ref} />);
return <Tab component={MyLink}
to={link.link}
label={link.label}
value={link.link}
key={link.link}
/>;
}
但我仍然收到警告。我该如何解决这个问题?
最佳答案
只需将其指定为innerRef
,
// Client.js
<Input innerRef={inputRef} />
将其用作引用
。
// Input.js
const Input = ({ innerRef }) => {
return (
<div>
<input ref={innerRef} />
</div>
)
}
关于reactjs - 使用react-router-dom时如何避免 'Function components cannot be given refs'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56484686/