简体   繁体   English

类型“字符串”不可分配给 react-router-dom 链接组件的类型

[英]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.

相关问题 react-router-dom元素类型无效 - react-router-dom Element Type Invalid 如何用 React 组件、react-router-dom 链接或锚标记替换位或字符串<a></a> - How can I replace bits or string with React component, react-router-dom Link or an anchor tag <a></a> 在链接路径中使用“#”的React-router-dom无法导航到组件 - React-router-dom using “#” in Link path does not navigate to Component React-Router-Dom`Link`更改路由,但没有组件加载 - React-Router-Dom `Link` changes route, but no component loads React-Router-DOM:无法在同一页面(组件)中呈现链接 - React-Router-DOM: unable to render Link in the same page (component) 如何根据 react-router-dom 单击的链接更改组件 - How to change a component on the basis of the link clicked by react-router-dom React-Router-Dom<link> 标签正在更改 URL 但未呈现组件 - React-Router-Dom <Link /> tag is changing URL but not rendering the Component 警告:失败的道具类型:提供给“路由”的无效道具“组件”-react-router-dom - Warning: Failed prop type: Invalid prop 'component' supplied to 'Route' - react-router-dom 警告:失败的道具类型:提供给“路由”的“对象”类型的无效道具“组件”,预期的“函数”使用 react-router-dom 错误 - Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` Error using react-router-dom 标签链接错误,react-router-dom - Error with the tag Link, react-router-dom
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM