I've made a GET request to the News API and received data as an object. Since the map method can only work on an array, I then use map on data.articles
as it's an array. (checked in the console log on Line 22).
With that said, I don't understand why I'm still receiving
TypeError Cannot read property 'map' of undefined
https://codesandbox.io/s/elated-star-j9s8g?file=/src/App.js
function App() {
const [apiUrl] = useState("https://gnews.io/api/v3/search?");
const [apiKey] = useState("123456789");
const [data, setData] = useState([]);
const [query, setQuery] = useState("");
const [url, setUrl] = useState(
`https://gnews.io/api/v3/top-news?token=95e7679f627d5796aa24f6692def5df3`
);
useEffect(() => {
const fetchData = async () => {
const result = await axios(url);
setData(result.data);
};
fetchData();
}, [url]);
const searchHandler = () => {
setUrl(`${apiUrl}q=${query}&token=${apiKey}`);
console.log(data.articles);
};
let newsNodes = data.articles.map((item) => {
return (
<li key={item.publishedAt}>
<a href={item.url}>{item.title}</a>
</li>
);
});
return (
<>
<input
type="text"
value={query}
onChange={(event) => setQuery(event.target.value)}
/>
<button type="button" onClick={searchHandler}>
Search
</button>
<ul>{newsNodes}</ul>
</>
);
}
useState
is set to[]
. – Kedarnag Mukanahallipatna