Api->index.js
import axios from 'axios';
const url = 'https://api.covid19india.org/data.json';
export const fetchDailyData = async () => {
try {
const response = await axios.get(url);
return response.data.statewise;
} catch (error) {
return error;
}
};
Body.js
class Body extends Component {
state = {
dailyData: []
};
componentDidMount()
{
const fetchMyAPI = async () => {
const initialDailyData = await fetchDailyData();
// console.log(initialDailyData); // giving correct data that is an Array
this.setState({
dailyData: initialDailyData,
})
console.log(dailyData) // gives error : dailyData is not defined
// console.log(this.dailyData) // gives undefined
};
fetchMyAPI();
}
My API is working well it is returning what I want but when I'm trying to set the dailyData variable and after it when I'm trying to access it it is giving undefined or either giving error(when I'm using it without this..)
Comments
Post a Comment