const TechnicalAnalysisIndicators = ({ data: initialData, width, ratio }) => {
const [data, setData] = useState(initialData);
const [rawData, setRawData] = useState();
const fetchData = async () => {
const response = await axios.get(
`https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=IBM&apikey=Y7UDL0WGHBS74NXI`
);
setRawData(response.data['Time Series (Daily)']);
console.log(rawData);
//here raw data is returning undefined value
};
useEffect(() => {
fetchData();
console.log('raw data'+rawData)
//here also raw data is returning undefined value
}, [rawData])
为什么原始数据没有从API获取数据.如果我在浏览器中粘贴api调用url,它会给结果setRawData(response.data),这一行是绝对正确的.我已经测试过了,它给出了响应.但是如何解决从api获取值后状态没有更新的问题?
1条答案
按热度按时间d7v8vwbk1#
这段代码中使用的API限制为每分钟5次调用。当我尝试这样做时,我得到了以下消息:
Thank you for using Alpha Vantage! Our standard API call frequency is 5 calls per minute and 500 calls per day. Please visit https://www.alphavantage.co/premium/ if you would like to target a higher API call frequency.
除此之外,当你在
useEffect
的dependency数组中使用rawData
时,它会一直获取,如果只获取一次,你需要传递一个空数组。我在这里格式化了代码:https://codesandbox.io/s/jolly-monad-ew458z?file=/src/App.js