I have a JSON file :
[
{
"level_1": "music",
"level_2": "rock",
"level_3": "had-rock / metal"
},
{
"level_1": "music",
"level_2": "rap",
"level_3": "rapcore / rap-west-coast"
},
{
"level_1": "music",
"level_2": "country",
"level_3": "contry-rock / country-pop"
}
]
I want render a select, for this I call my json file:
const [musicType, setMusicType] = useState([]);
useEffect(() => {
axios
.get("https://www.musictype")
.then(function (response) {
setMusicType(response.data);
})
.catch(function (error) {
console.log('request failed', error)
})
}, []);
And i create a map function on the return :
<select>
<option value="" placeholder="Selection">Selection</option>
{musicType.map((type, index) => (
<option key={index}>{type.level_3}</option>))}
</select>
But it isn't render alphabetically, if I Use sort() React display an error:
<select>
<option value="" placeholder="Selection">Selection</option>
{musicType.sort((a,b) => (
<option key={index}>{a.level_3.localeCompare(b.level_3)}</option>))}
</select>
"Error: valid as a React child (found: object with keys {level_1, level_2, level_3}). If you meant to render a collection of children, use an array instead."