I have a functional component that retrieves data from an http request on page load and updates the state to store that data using useState. I then try to map out a nested array (polls.pollOptions) from the data, I get an error: "TypeError: Cannot read property 'map' of undefined". If i remove the map function and check the React component extension, I can see that the state was updated and it has the correct data. So why doesn't this render to the screen & why receive an error when the data is there to map through? Heres my component:
import React, { useState, useEffect } from "react";
import axios from "axios";
import { useParams } from "react-router";
export const EditPolls2 = () => {
const { _id } = useParams();
const [polls, setPolls] = useState([]);
useEffect(() => {
axios.get(`/polls/${_id}`).then((p) => {
setPolls(p.data);
});
}, [_id]);
return (
<div>
<p>edit polls 2</p>
<div>
<ul>
{polls.pollOptions.map((p) => (
<li key={p.pollId}>{p.option}</li>
))}
</ul>
</div>
</div>
);
};