I am currently working on populating Material's UI with a list of countries using the following code:
import React from "react";
import FormControl from "@material-ui/core/FormControl";
import InputLabel from "@material-ui/core/InputLabel";
import Select from "@material-ui/core/Select";
import MenuItem from "@material-ui/core/MenuItem";
import countries from "./data";
const renderCountrySelect = props => {
return (
<>
<FormControl>
<InputLabel id="countrySelectLabel">Country</InputLabel>
<Select labelId="countrySelectLabel" id="countrySelect" value=''>
{countries.map(({ code, name }, index) => (
<MenuItem key={index} value={code}>
{name}
</MenuItem>
))}
</Select>
</FormControl>
</>
);
};
export default renderCountrySelect;
Using an uncontrolled component for brevity. However, I encountered the following error:
Encountered two children with the same key, `.$.$.$[object Object]`. 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.
Below is a snippet from the data.js
file:
export default [
{ code: "AD", name: "Andorra" },
{ code: "AE", name: "United Arab Emirates" },
{ code: "AF", name: "Afghanistan" },
{ code: "AG", name: "Antigua and Barbuda" }
];
What could be the issue here?
UPDATE: I changed the key
from code
to index
, but the error persists.