export const FetchDailyData = () => {
try {
const [data, setData] = useState({ numbers: [], isFetching: false });
useEffect(() => {
const fetchData = async () => {
setData({ ...data, isFetching: true });
const response = await axios.get(`${apiURL}/daily`);
setData({
...data,
numbers: response.data.slice(0, 50),
isFetching: false
});
};
fetchData();
}, [data]);
console.log(`data= ${data.numbers} isFetching= ${data.isFetching}`);
} catch (error) {
if (error) throw error;
console.log(error);
setData({ ...data, isFetching: false });
}
}
Please can anyone help me fix this issue? I tried to fetch api data but this error I cannot handle.
You can't wrap useEffect in anything that might cause it not to run, including try/catch. Looking at what you are doing, this is probably a better option:
export const FetchDailyData = () => {
const [data, setData] = useState({ numbers: [] });
const [fetching, setFetching] = useState(false);
useEffect(() => {
const fetchData = async () => {
const response = await axios.get(`${apiURL}/daily`);
setData({
...data,
numbers: response.data.slice(0, 50),
});
}
try {
setFetching(true);
fetchData();
} catch (error) {
console.error(error);
} finally {
setFetching(false);
}
}, [data]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With