JsonData
[
{
"id": "2",
"name": "Ajay",
"role": "Employee",
"department": "Accounts Department",
"ratings": "4",
"joiningdate": "2 Jan 2021"
}
]
This is the App
<React.Fragment>
<Routes>
<Route path="/employee/:id" element={<Employee />} />
</Routes>
</React.Fragment>
Inside employee component i am using
let empId=useParams()
i get empID but its in this format {id:"2"}
what changes should i do to make only 2 so that i can to pass in api req as below
axios
.get(`http://localhost:8000/EmployeeData/${empId}`)
.then((res) => {
console.log(res.data);
})
.catch((err) => {
console.log(err);
});
CodePudding user response:
Convert the id to number Number(res.data.id) Number("123")
CodePudding user response:
You can destructure object from useParams:
const {id:empId} = useParams();
or you can simply use dot notation in get()
.get(`http://localhost:8000/EmployeeData/${empId.id}`)
CodePudding user response:
The data type of empId is an object so you can get the id with empId.id
Please find the reference here
CodePudding user response:
you can use parseInt(res.data.id) or Number(res.data.id)
