[英]Type 'string' is not assignable to type for react-router-dom Link Component
How the hell do I get rid of this TS error referring to the ref="link"
prop?我到底如何摆脱这个引用
ref="link"
道具的 TS 错误?
Type 'string' is not assignable to type '((instance: HTMLAnchorElement | null) => void) | RefObject<HTMLAnchorElement> | null | undefined'.
import {Link} from 'react-router-dom';
CompanyList.tsx公司列表.tsx
export default class CompanyList extends Component<{ companies: Array<Company>, countries: Array<Country> }, any> {
const link = (
<Link className={className} id={company.id} ref="link" to={uri}>
<span id="company-name">{company.name}</span>
</Link>
);
... rest of component
Here is another look at it from the IDE in another component:这是另一个组件中的 IDE 的另一个视图:
The ref
cannot be a String
. ref
不能是String
。 It needs to be created using React.createRef()
.它需要使用
React.createRef()
创建。 Please see: https://reactjs.org/docs/refs-and-the-dom.html#creating-refs请参阅: https://reactjs.org/docs/refs-and-the-dom.html#creating-refs
Refs are created using
React.createRef()
and attached to React elements via the ref attribute.Refs 使用
React.createRef()
创建并通过 ref 属性附加到 React 元素。 Refs are commonly assigned to an instance property when a component is constructed so they can be referenced throughout the component.Refs 通常在构建组件时分配给实例属性,以便可以在整个组件中引用它们。
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
}
render() {
return <div ref={this.myRef} />;
}
}
Also note that if you're using React Router < v5.1, you need to use innerRef
to access the DOM element.另请注意,如果您使用的是 React Router < v5.1,则需要使用
innerRef
来访问 DOM 元素。 See it here: https://reactrouter.com/web/api/Link/innerref-function在这里看到它: https://reactrouter.com/web/api/Link/innerref-function
As of React Router 5.1, if you are using React 16 you should not need this prop (
innerRef
) because we forward the ref to the underlying<a>
.从 React Router 5.1 开始,如果你使用的是 React 16,你不应该需要这个 prop (
innerRef
),因为我们将 ref 转发到底层的<a>
。 Use a normal ref instead.请改用普通的 ref。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.