I have spring boot app for "backend" in order to retrieve data for stocks from yahoo.finance. I have tested the app with postman and retrieves the data. However, I can't make the React ui to present them and the error that I get is 'TypeError: stocks.map is not a function '. My React class:
import { React, useEffect, useState } from 'react';
export const StockPage = () => {
const [stocks, setStocks] = useState({});
useEffect(
() => {
const fetchStockData = async () => {
const response = await fetch("http://localhost:8080/stock/AAPL/2021-06-07/2021-06-11");
const stockData = await response.json();
setStocks(stockData)
console.log(stockData);
};
fetchStockData();
},[]
);
return (
<div className="StockPage">
<h2>Stock Data</h2>
<div className="header-section">
{stocks.map(stock => <p>{stock.date} {stock.open} {stock.high} {stock.low} {stock.close} {stock.adjClose} {stock.volume}</p>)}
</div>
</div>
);
}
Also, the console.log(stockData) prints out the array of stock object in browser console.