0

I'm working on a reactjS PROJECT and I want To get the id of the selected item in a link to fetch the details. this is how I consume the api :

const [selectedComplain, setSelectedComplain] = useState([]);

async function getComplain(compl_id) {
  setSelectedComplain(compl_id)
  console.warn(compl_id)
  let result = await fetch(`${API_ENDPOINT}/api/getComp/` + compl_id);
  result = await result.json();
  console.warn(result)
}

This is the Link part :

<Link to={`/complain/details/`} compl_id={selectedComplain}>
  <span onClick={()=>getComplain(item.compl_id)}>
    {item.compl_title}
  </span>
</Link>

Thanks in advance for anyone will try to help me

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
user
  • 1
  • 2
  • ***Where*** are you wanting to get an id value from? Can you explain what isn't working as expected? – Drew Reese Apr 11 '22 at 23:05
  • @DrewReese in the next page which has path '/complain/details/' . Actually I'm working on notification I want when I click on a specifi item I redirect to another page which has the details of the selected item in notification – user Apr 11 '22 at 23:07
  • place the id inside the path `/complain/details/${selectedComplain.id} then in your /complain/details/:id you can use the useParams hook that is provided by react-router-dom to get the :id. but first you must set it in your routes. E.g. – Dylan L. Apr 11 '22 at 23:12
  • @DylanL. I followed your instructions bu for the id I get always undefined – user Apr 11 '22 at 23:36
  • Make sure and take a look at this documentation. It's the best I can do for you now that the question is closed. https://reactrouter.com/docs/en/v6/getting-started/tutorial#reading-url-params https://reactrouter.com/docs/en/v6/api#useparams – Dylan L. Apr 11 '22 at 23:44

0 Answers0