0

I want to update the time every minute without refreshing the whole page.

This is a part from the code which I used from github:

const fetchData = async () => {
setIsLoading(true);
try {
const forecastResponse = await axios.get(endpoint, { params });
const payload = mapData(
forecastResponse.data.daily,
forecastResponse.data.current,
lang,
      );
dispatch({
type: SUCCESS,
payload,
      });
    } catch (error) {
console.error(error.message);
dispatch({ type: FAILURE, payload: error.message || error });
    }
setIsLoading(false);

  };

useEffect(() => {
fetchData(); 
const interval = setInterval (() => {
fetchData()
    }, 60000)
return() => clearInterval(interval)
 }, [lon, lat]);
return { data, isLoading, errorMessage, fetchData }; 
};

I tried to solve the problem by adding

 "const interval = setInterval (() => {
fetchData()
    }, 60000)"

in useEffect but website still does a refresh. How can I do it with the corret way that only the time, every minute, changes?

Apostolos
  • 10,033
  • 5
  • 24
  • 39
  • I can't see any code here related to page refresh. Can it happen that the page refresh logic is written somewhere else ? – kumarmo2 Jul 14 '21 at 07:04
  • No, there is no other function. Only the setInterval function updates the time and weather, but for a second the whole page loads again while start the fetchData function in the setinterval function. – wantstothinkandgehtthesolution Jul 14 '21 at 07:09

0 Answers0