I am making an api call and setting all the data in my redux state. Now when I refresh the page, all the data in redux gets cleared and my data is lost. So should I add a check on each page whether data is present in state and if not then make an api call again?
const refreshApiCall = async () => {
if (coinsData === []) {
const coins = await getListOfCoins();
}
const userWallet = await getUserCoins();
dispatch(setWallet(userWallet));
}
useEffect(() => {
console.log('inside use effect');
console.log(coinsData);
refreshApiCall();
}, []);
This is sort of what I have been doing
2
Answers
you can follow these solutions to solve your problem, if you need more detail, etc you can tell me, good luck.
problem1:
Solution:
note
problem2
guide
PROBLEM:
All the data in redux gets cleared and my data is lost.
ANSWER:
In realtime problems the data keeps changing and therefore you should not save the api data. You should only save data that will not change for example (profile data,configuration etc.)
For this install necessary package "redux persist".
Your reducer will be changed with this:
And Main.js will be changed as :