react-router - 错误:未捕获 [TypeError:无法读取未定义的属性“id”]
问题描述
我正在使用 react-router-dom 来路由我的组件并对一个路由组件进行测试。我App.js
有
<Route
path="/register/:id"
render={(props) => (
<Details
{...props}/>
)}
></Route>
我试图像这样将匹配添加到组件中
const match = {
params: {
id: "123"
}
};
Details
组件在某些函数中使用,this.props.match.params.id
我不知道这些函数是如何调用的,但是我得到了错误
Error: Uncaught [TypeError: Cannot read property 'id' of undefined]
当我使用
console.log(this.props.match)
我明白了
{ match: { params: { id: 'id' } } }
但是当我使用
console.log(this.props.match.params)
我明白了undefined
。
更新
我认为我传递匹配参数的方式是错误的。在我创建一个 json 对象posts
并将该 json 对象传递给这样的测试组件之前
mount(<Details {...posts}/>)
然后我改变方式
mount(<Details {...posts} match={match}>)
它有效。
解决方案
推荐阅读
- svelte - 仅在客户端运行某些代码的最佳方法是什么?
- javascript - 未捕获的 TypeError:document.getElementById(...).addEventHandler 不是函数
- java - 从字符串中检索具体数据
- apache - 重定向到 https://www 与 RewriteBase 一起使用时如何修复未找到错误?
- embedded - 是否可以在闪存驱动器(笔式驱动器)上编写程序来执行将闪存驱动器插入 USB 端口的任务?
- javascript - 如何使用 go buffalo 框架渲染 quill js 内容
- python - 使用 ccrs.epsg() 绘制带有 EPSG 4326 坐标系的邮政编码周长 shapefile
- mql4 - 将 .tpl 文件类型合并到 TradingView 对图表中
- c# - 在运行时在 blazor 托管中编辑 .razor 文件
- angular - Firebase firestore 不适用于 Angular Universal:package.json 不存在