JavaScript
x
21
21
1
import React, { useState } from 'react'
2
export const BreedsSelect = props => {
3
const [selectedBreed, setSelectedBreed] = useState(null)
4
5
const options = props.breeds
6
7
return (
8
<div>
9
<select
10
value={selectedBreed}
11
onChange={e => setSelectedBreed(e.target.value)}
12
>
13
{options?.map(breed => {
14
;<option>{breed}</option>
15
})}
16
</select>
17
</div>
18
)
19
}
20
21
In the above code I get an error that options.map is not a function. How can this be resolved? The following code calls the above code.
JavaScript
1
14
14
1
import React, { useEffect, useState } from 'react' // DO NOT DELETE
2
import { BreedsSelect } from './BreedsSelect'
3
export const DogListContainer = () => {
4
const [breeds, setBreeds] = useState(null)
5
useEffect(() => {
6
fetch('https://dog.ceo/api/breeds/list/all')
7
.then(res => res.json())
8
.then(data => {
9
setBreeds(data.message)
10
})
11
}, [])
12
return <BreedsSelect breeds={breeds} />
13
}
14
Advertisement
Answer
im trying it and it cause of the data was late to read and the browser was too fast to read it so i tryin with this
JavaScript
1
26
26
1
import React, { useEffect, useState } from "react"; // DO NOT DELETE
2
import { BreedsSelect } from "./BreedsSelect";
3
export const DogListContainer = () => {
4
const [breeds, setBreeds] = useState(null);
5
useEffect(() => {
6
const fetchAll = async () => {
7
try {
8
const data = await fetch("https://dog.ceo/api/breeds/list/all");
9
if (!data.ok) throw new Error("error");
10
return data;
11
} catch (error) {
12
return error;
13
}
14
};
15
16
fetchAll()
17
.then((res) => {
18
return res.json();
19
})
20
.then((data) => {
21
setBreeds(data.message);
22
});
23
}, []);
24
return <BreedsSelect breeds={breeds} />;
25
};
26
idk if it woukd be helps u or no