简体   繁体   中英

How do I use the params on my browser url on axios GET request

Currently, I have been able to get the data into my component using

let {id} = useParams();

And when I call

{id} within my component, I get the id from the browser,s url. I however need it to enable me fetch data from my db

I have tried

const getData = () => {
     axios.get(`/API/joindiscussion/` + {id}, {
     //do something
     }):
}

But it didn't work

nodejs api route /API/joindiscussion/:id

passed in react <Router path="/exmple/:id" element={} />

Example component used const id =useParams()

const getData = () => {

 axios.get(`/API/joindiscussion/${id}`, {
 //do something

 }):

}

OR

nodejs api route /API/joindiscussion?id=123

const getData = () => {

 axios.get(`/API/joindiscussion?${id}`, {
 //do something
 }):

}

this above concept in ES6 javascript

Check your API and how it takes the id ( I'm assuming you're confused between the param and query method ).

If the API takes the id in param format, ( like: /API/joindiscussion/1234 where 1234 is the id) then your code should be

axios.get(`/API/joindiscussion/` + id,

If the API takes the id in query format ( like: /API/joindiscussion/?id=1234 where 1234 is the id ) then your code should be

axios.get(`/API/joindiscussion/?id=` + id,

comment if you have any doubts.

Something like this:

let param = useParams()
  axios.get(`url/${param.id}`).then(response => {
    // do something with the response
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM