I'm trying to build a website with REACT. In the homepage you have 2 buttons, europe and usa. Let's say you click europe. Then you see a list of all the countries in europe. And when you click a country, you should see a list of CITIES in that country.
The question is, how can I access the items inside "cities"?.
const DATA = [
{
id: 1,
title: "EUROPE",
countries: [
{
id: 1,
country: "france",
cities: [
{
id: 1,
city: "paris"
},
{
id: 2,
city: "toulouse"
}
];
// so at homepage, you click "europe", and on the second page i got this:
const StateCard = () => {
const { title } = useParams();
const selectedData = DATA.find( d => d.title === title);
return(
<div className="main">
{selectedData &&
selectedData.countries.map((item, id) => {
return (
<div className="card-container" >
<Link key={id} to={`${title}/${item.country}`}> {item.country} </Link>
</div>
);
})}
</div>
useParams gives us back the title that added to the URL after the first click, which is "europe". selectedData gives us back the items inside "europe": {id: 1, title: "EUROPE", countries: Array(1)}
and now the screen shows "france". you clicked france, and now i wanna show the 2 cities inside. all i got is:
const { country } = useParams();
which gives us "france". but i dont know how to access the cities inside. i tried to play with DATA.countries.find(), but whatever i put after DATA. gives me "TypeError: Cannot read property 'find' of undefined".
sorry its so long thanks guys!