首页 > 解决方案 > Problem trying to put a variable value into my Link to value for the URL

问题描述

I am getting this error when trying to construct a URL in my link component:

bundle.js:2567 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

Here is what I have in my React component:

_renderSubNav() {
  const { dispatch } = this.props

  let userId = this.props.match.params.userId;
  console.log('userId is' + userId);

  return (
    <div className="">
        <nav aria-label="breadcrumb">
            <ol className="breadcrumb">
                <li className="breadcrumb-item"><Link to={`/users/${userId}`} className="nav-link">User</Link></li>
            </ol>
        </nav>
    </div>
  );
}

The console does log the id in chrome, so userId is not undefined it is e.g. 2. How can I put the userId into the Link component?

标签: reactjs

解决方案


你需要使用

import {Link} from 'react-router-dom';

重要:{Link}而不是Link. 这是您的代码和框中的错误,很可能也是您的实际代码中的错误。


推荐阅读