Trying to fetch data from the NHL API, using React Hooks.
Link to CodeSandbox.
When I comment out the tag and it's content in the return statement, my console log does fetch the API successfully. However, I'm getting an error in the console when the tag's content (the .map statement), saying "Cannot read property 'map' of undefined".
Trying to figure out why this error is displaying, and why I can't map the data defined from the API.
App.JS
import React, { useState, useEffect } from "react";
import ReactDOM from "react-dom";
import axios from "axios";
import "./styles.css";
function App() {
const [data, setData] = useState({ people: [] });
useEffect(() => {
const fetchData = async () => {
const result = await axios(
"https://statsapi.web.nhl.com/api/v1/people/8475166?expand=person.stats&stats=yearByYear,careerRegularSeason&expand=stats.team&site=en_nhlCA"
);
setData(result.data.people);
console.log(result.data.people);
};
fetchData();
}, []);
return (
<ul>
{data.people.map(item => (
<li key={item.objectID}>
<p>{item.id}</p>
<p>{item.primaryNumber}</p>
</li>
))}
</ul>
);
}
export default App;
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);