【发布时间】:2021-10-25 16:34:14
【问题描述】:
我有两个页面,我想将数据从一个功能组件传递到另一个,我试图通过使用 <Link/> 来实现这一点,但我不知道如何在我的另一个页面中检索它:
这是我的代码:
首页:
const CustomerListResults = () => {
<Link
to={{
pathname: "/app/CustomerDetails",
data: id // your data array of objects
}}>
<Button
color="primary"
variant="contained"
>
Expand
</Button>
</Link>
}
我需要数据的第二页:
const CustomerProfile = () => {
// here i want to get the id i passed in the <Link/> component
}
我的 Routes.js 文件:
const routes = [
{
path: 'app',
element: <DashboardLayout />,
children: [
{ path: 'account', element: <Account /> },
{ path: 'customers', element: <CustomerList /> },
{ path: 'CustomerDetails', element: <CustomerDetails /> },
{ path: 'ReportDetails', element: <ReportDetails /> },
{ path: 'report', element: <ReportList /> },
{ path: 'freight', element: <FreightList /> },
{ path: 'dashboard', element: <Dashboard /> },
{ path: 'analytics', element: <Analytics /> },
{ path: 'products', element: <ProductList /> },
{ path: 'settings', element: <Settings /> },
{ path: '*', element: <Navigate to="/404" /> }
]
},
{
path: '/',
element: <MainLayout />,
children: [
{ path: 'login', element: <Login /> },
{ path: 'register', element: <Register /> },
// { path: '404', element: <NotFound /> },
{ path: '/', element: <Navigate to="/login" /> },
{ path: '*', element: <Navigate to="/app/dashboard" /> }
]
}
];
export default routes;
【问题讨论】:
-
作为道具发送
-
@ABDULLOKHMUKHAMMADJONOV 你能给我一个例子代码吗?
标签: javascript reactjs react-router react-router-dom