This is my component:
import React, { useState, useEffect } from "react";
export default function App() {
const [countriesArray, setCountriesArray] = useState([]);
useEffect(() => {
getCountriesArray();
}, []);
const getCountriesArray = async () => {
try {
let response = await fetch(
"https://coronavirus-19-api.herokuapp.com/countries"
);
if (response.status === 200) {
const newCountriesArray = [...countriesArray];
const data = await response.json();
await data.forEach(item => newCountriesArray.push(item.country));
setCountriesArray(newCountriesArray);
} else {
setErrorStatus(true);
console.error("Error status");
}
} catch (err) {
console.error(err);
}
};
const optionItems = countriesArray.map((item) =>
<option key={item}>{item}</option>
)
return (
<div className="App">
<select>{optionItems}</select>
</div>
);
}
In the select I get the names of the countries when mounting the component but in the console I have a loop error message:
Warning: Encountered two children with the same key, `Total:`. Keys should be unique so that components maintain their identity across updates. Non-unique keys may cause children to be duplicated and/or omitted — the behavior is unsupported and could change in a future version.
in select (at App.js:36)
in div (at App.js:35)
in App
in StrictMode (at src/index.js:8)
However I use the empty array as a second parameter of the useEffect to execute it only when mounting the component