【问题标题】:React js Error with property 'params' of undefined使用未定义的属性“参数”反应 js 错误
【发布时间】:2021-04-30 19:29:43
【问题描述】:

我正在尝试获取 id,但显示此错误:

TypeError: Cannot read property 'params' of undefined

这是来自 list.js 的链接

.
.
.
import { Link } from "react-router-dom";
.
.
.

<Link to={`/edit/${item.id}`} > Edit </Link>
.
.
.

这个是组件edit.js

.
.
.



   import { Link, useHistory } from "react-router-dom";
    .
    .
    .
    const Edit = (props) => {
      .
      .
    const history = useHistory();
    const currentCatId =  props.match.params.id; 

    .
    .
    .
} export default Edit;

错误就在这里 props.match.params.id

我做错了什么??

【问题讨论】:

    标签: reactjs jsx


    【解决方案1】:

    如果您使用 useHistory,请使用 useParams,在您的情况下:

    const { currentCatId } = useParams();
    

    别忘了 (:):

    <Route path="/edit/:currentCatId ">
        <Edit/>
    </Route>
    
    
    <Link to={`/edit/${item.id}`} > Edit </Link>
    

    【讨论】:

    • 不能解构currentCatId,应该是id
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2021-11-20
    • 2022-07-26
    • 1970-01-01
    • 2016-07-15
    • 2017-05-12
    相关资源
    最近更新 更多