0

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

3 Answers 3

2

NodeJS API route: /API/joindiscussion/:id

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

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 JS

Sign up to request clarification or add additional context in comments.

1 Comment

This resolved my challenge. Thank you very much.😊
0

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.

Comments

0

Your code could look like this:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.