I want to pass a specific user's id from a list of users that I have in my Dashboard. I have a button that is called Expand, when I click this button it would take me to another page that would show me all the user's info and details.
I am trying to pass the user's id to be able to fetch the specified user's info from my DB
I am moving from CustomersList Page when I click the expand button to CustomerProfile Page.
but on the second page, I get the value as undefined.
My Code:
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>
)
and in my second page:
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
})
},[]);
}
what am i doing wrong?
EDIT:
this is my routes.js file:
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" /> }
]
}
];