reactjs - 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?
解决方案
你需要使用
import {Link} from 'react-router-dom';
重要:{Link}
而不是Link
. 这是您的代码和框中的错误,很可能也是您的实际代码中的错误。
推荐阅读
- dialogflow-es - 使用 Java API 时文本转语音不起作用
- scala - 不满足条件时,二叉搜索树中的 contains 方法如何返回 false?
- jquery - 错误 DataTable 不是 Electron 中的函数
- python - TensorFlow 强化学习模型几乎不会自行做出决定,也不会学习。
- python - 如何忽略传递给打印函数的未定义变量?
- r - 过滤作为R中另一个时间序列数据范围的时间序列数据
- c# - 我可以在 => 运算符之后组合多个对方法的调用吗?
- vim - 在vim中,如何在文件末尾启动vim以及-c可以使用哪些命令
- docker - docker从局域网访问容器
- mysql - MySQL group by 填补空白