Skip to content
Advertisement

React.js send id from or pass props to other component

I have this List of albums when i click on it, I want the new Page to have the data from this album.id. I want the Information of the album with the id I clicked on in the component Cover.

Basically:

  1. I click on the ListItem with the album.id 61a947a868f7d778e3ee73f8
  2. My route /ItemPage opens
  3. /ItemPage has the Cover Component
  4. I want the Cover component to get the Information of the ListItem component
  5. /ItemPage renders each time with the information of the album I click on

For example :

ListItem.jsx

JavaScript

…….

Now I have this album item where the id is : 61a947a868f7d778e3ee73f8

My ItemPage.jsx looks like this

JavaScript

};

Cover.jsx

JavaScript

App.jsx Route

JavaScript

Advertisement

Answer

You will have to define a dynamic route for list if you want to access the id from URL App.jsx:

JavaScript

In ItemPage.jsx component you can use useParam hook from react-router-dom to access the selected id.

JavaScript

and then in itempage you can call the API to get the selected item data.

Advertisement