我知道如果我需要呼叫另一個,component那么我們需要傳遞 like<A x={y}/>并且我們可以訪問props.xinside A。
但在這里我需要打電話,EditCertificate所以我需要傳遞id給EditCertificate. 但我在Link這里使用。我無法通過id. 當我訪問它時,它來了undefined。
<Link to={`/${props.certificate.id}/edit` } >Edit</Link>
我正在呼叫這個頁面,如下所示。
<Route path ="/:id/edit" component={EditCertificate} ></Route>
我怎樣才能訪問:id里面EditCertificate,我訪問它給。當undefined。我是否需要傳遞一些其他屬性。
uj5u.com熱心網友回復:
由于EditCertificate由路由直接呈現:
<Route path ="/:id/edit" component={EditCertificate} />
該路線道具傳遞給EditCertificate。你只需要從 props 訪問它們。
const { id } = props.match.params;
如果EditCertificate是一個類組件,那么顯然可以從this.props.
const { id } = this.props.match.params;
uj5u.com熱心網友回復:
由于您使用的是 react-router,您可以簡單地匯入useParams鉤子并在您的EditCertificate組件中獲取 id
import { useParams } from 'react-router-dom';
const { id } = useParams(); // add inside your component body
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/372516.html
標籤:javascript 反应 打字稿 用户界面
下一篇:使用正則運算式洗掉部分字串并替換
