我想将 id 参数传递给 showdesc 函数,但没有传递 任何人都可以帮忙吗
app.js 有一个路由器
<Router>
<Route exact path="/">
{/*sets the header to display searchbar*/}
<Header isSearchBar={true} handleOnChange={change} />
<ShowCards cards={movies} lastMovieRef={lastMovieElement}/>
</Route>
<Route exact path="/:id">
{/*sets the header to not display searchbar*/}
<Header isSearchBar={false} handleOnChange={change} />
<ShowDesc/>
</Route>
</Router>
showdeck.js
import * as React from 'react'
import './ShowDesc.css'
import { useState,useEffect } from 'react';
import { useParams } from 'react-router-dom';
function ShowDesc(){
const [card,setCard] =useState();
const {handle} = useParams();
return (
<p>handle</p>
)
}
参数 id 没有传递给 showDesc 函数,谁能帮我把它传递出去
答案 0 :(得分:0)
首先,您应该使用 id
而不是 handle
,因为参数是 id,其次您应该使用花括号 (<p>{id}</p>
) 将 id 放在 HTML 中以显示值,像这样:
const {id} = useParams();
return (
<p>{id}</p>
)
或者:
const params = useParams();
return (
<p>{params.id}</p>
)
您可以在 react router dom homepage 中查看更多文档。