axios.get('https://api.covid19india.org/data.json')
.then(response => {
if (response !== undefined && response.data.statewise.length>0) {
self.setState({ data: response.data.statewise }, () => {
self.getTestingData()
})
}
})
当我在heroku上部署应用程序时,页面未加载(https://asciiclan-covid19.herokuapp.com/)
显示无法读取未定义长度并参考上述代码。
答案 0 :(得分:-1)
使用以下代码
import React , {useEffect, useState}from 'react';
import Axios from 'axios';
const Covid = () => {
const [states, setStates] = useState([]);
useEffect(() =>{
Axios.get('https://api.covid19india.org/data.json').then(res => {
if (res !== undefined && res.data.statewise.length>0) {
setStates(res.data.statewise );
}
}
);
} , []);
const state = states.map((item, index) => {
return(
<p key = {index}>{item.state}</p>
);
})
return (
<div>
{state}
</div>
)
}
export default Covid