Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  95] [ 6]  / answers: 1 / hits: 10633  / 4 Years ago, sat, february 29, 2020, 12:00:00

I'm trying to populate Material's UI with a list of countries, like so:



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 simpleCountrySelect = 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 simpleCountrySelect;


Uncontrolled component for the brevity sake. But I'm getting 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.


Here is sample of 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 am I doing wrong here?



EDIT: changed key from code to index, still nothing.


More From » reactjs

 Answers
3

It seems that you meant to destructure each object in the array but you've missed the curly braces:



countries.map(({code, name}) =>

[#4604] Tuesday, February 25, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
raveno

Total Points: 453
Total Questions: 92
Total Answers: 92

Location: France
Member since Thu, Oct 27, 2022
2 Years ago
;