I'm using https://restcountries.eu/rest/v2/all to fetch all the names of countries.
I'm trying to generate a random country name on a click. I've mapped through the array of object to get the names of countries, but onClick
doesn't work.
const WorldCard = () => {
const [country, setCountry] = useState([])
const [name, setCount] = useState(0)
useEffect(() => {
fetch('https://restcountries.eu/rest/v2/all')
.then(res => res.json())
.then(result => {
setCountry(result)
console.log(result)
})
}, [])
const countryName = country => {
return country.map(d => d.name)
}
return (
<Card>
<Card.Body>{name}</Card.Body>
<Button
onClick={() =>
setCount(countryName[Math.floor(Math.random() * countryName.length)])
}
>
Click me
</Button>
</Card>
)
}
Any help will be appreciated! Thanks!
Aucun commentaire:
Enregistrer un commentaire