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:
- I click on the ListItem with the album.id 61a947a868f7d778e3ee73f8
- My route /ItemPage opens
- /ItemPage has the Cover Component
- I want the Cover component to get the Information of the ListItem component
- /ItemPage renders each time with the information of the album I click on
For example :
ListItem.jsx
JavaScript
x
31
31
1
function ListItem({index, item}) {
2
const [isHovered, setIsHovered] = useState(false);
3
const [album, setAlbum] = useState({});
4
5
6
useEffect(() => {
7
const getAlbum = async () => {
8
try{
9
const res = await axios.get("/albums/find/" + item , {
10
headers:{
11
token: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYxOThmM2YwNzdiYzI5ZTkyZTRlZDU5MCIsImlzQWRtaW4iOnRydWUsImlhdCI6MTYzODM2MzkyNSwiZXhwIjoxNjM4Nzk1OTI1fQ.LNyVCvxwCncOWhLS_uGAXBh-WX0GQffe3CeqcMzmZK4"
12
},
13
});
14
setAlbum(res.data);
15
} catch (e) {
16
console.log(e)
17
}
18
};
19
getAlbum();
20
},[item])
21
22
23
return (
24
<Link to={{ pathname:"/itemPage", album:album}}>
25
<div className={"listItem"}
26
style={{left: isHovered && index * 225 - 50 + index * 2.5}}
27
onMouseEnter={() => setIsHovered(true)}
28
onMouseLeave={() => setIsHovered(false)}>
29
30
{album.id}
31
…….
Now I have this album item where the id is : 61a947a868f7d778e3ee73f8
My ItemPage.jsx looks like this
JavaScript
1
9
1
const ItemPage = () => {
2
const [lists, setLists] = useState([]);
3
return (
4
<div className={"itemPage"}>
5
<Navbar/>
6
<Cover/>
7
</div>
8
);
9
};
Cover.jsx
JavaScript
1
23
23
1
return (
2
<div className={"cover"}>
3
<div className="coverImage">
4
<img src="{album.img}" alt=""/>
5
</div>
6
<div className="coverInfo">
7
<Card>
8
<CardContent>
9
<Typography textAlign={"center"} gutterBottom variant="h5" component="div">
10
</Typography>
11
<Typography variant="body2" color="white" textAlign={"center"} fontSize={150}>
12
{album.rating}
13
</Typography>
14
</CardContent>
15
{album.date} {album.title}
16
<div className="cardImages">
17
</div>
18
</Card>
19
</div>
20
<Watch/>
21
<Similiar/>
22
</div>
23
App.jsx Route
JavaScript
1
2
1
<Route path="/itemPage" element={<ItemPage/>}/>
2
Advertisement
Answer
You will have to define a dynamic route for list if you want to access the id from URL App.jsx:
JavaScript
1
2
1
<Route path="/itemPage/:item" element={<ItemPage/>}/>
2
In ItemPage.jsx component you can use useParam hook from react-router-dom to access the selected id.
JavaScript
1
5
1
import {
2
} from "react-router-dom";
3
4
let { item } = useParams();
5
and then in itempage you can call the API to get the selected item data.