I'm trying to map through some data that looks like this:
[
{
"id": "cambridgeshire",
"name": "Cambridgeshire"
},
{
"id": "bedfordshire",
"name": "Bedfordshire"
}
]
And display it as a list.
I'm getting the error of allAreas is undefined. I have a feeling its because of the JSON Object which I'm unfamiliar with.
On checking the state of allAreas it shows undefined.
Heres the code Ive tried
const Directory = () => {
const [allAreas, setAllAreas] = useState([]);
useEffect(() => {
fetchAllAreasData().then((data) => setAllAreas(data));
}, []);
return (
<div className='directory'>
<ul>
{allAreas.map((area) => (
<li key={area.id}>{area.name}</li>
))}
</ul>
</div>
);
};
To clarify this is an array of objects
allAreasis an empty objectuseState({}). UseuseState([])