【发布时间】:2021-10-24 11:28:55
【问题描述】:
我想从仪表板中的用户列表中传递特定用户的 ID。我有一个名为 Expand 的按钮,当我单击此按钮时,它会将我带到另一个页面,该页面将显示所有用户的信息和详细信息。
我正在尝试传递用户 ID,以便能够从我的数据库中获取指定用户的信息
当我单击展开按钮时,我正在从客户列表页面移动到客户资料页面。
但是在第二页上,我得到的值是未定义的。
我的代码:
const CustomerList = () => {
const navigate = useNavigate();
const [data, setData] = useState([]);
const [id, setID] = useState("");
const list = []
useEffect(() => {
firebase.firestore().collection("Users").get().then((userSnapshot) => {
userSnapshot.forEach((doc) => {
const {powerAccount,first_name,registerDate,email,company,country,phone} = doc.data();
list.push({
usersID:doc.id,
powerAccount:powerAccount,
first_name:first_name,
registerDate:registerDate,
email:email,
company:company,
country:country,
phone:phone,
});
})
setData(list);
console.log(list)
})
},[]);
return (
<Button
color="primary"
variant="contained"
onClick={()=>{
navigate('/app/CustomerDetails', { state: { id: data.usersID } });//this is how i am navigating and passing the value
}}
>
Expand
</Button>
)
在我的第二页:
const CustomerProfile = (props) => {
const {state} = useLocation();
const { id } = state; // Read values passed on state
console.log(id)//here i get this log as undefined
const [name,setName] = useState("");
useEffect(() => {
firebase.firestore().collection("Users").doc(id).get().then((userSnapshot) => {
console.log(userSnapshot.data())// here i get this log as undefined
})
},[]);
}
我做错了什么?
编辑:
这是我的 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" /> }
]
}
];
【问题讨论】:
标签: javascript reactjs react-router react-router-dom