I have this component:
export const Widget: FC<IWidgetProps> = ({ appid, flavor, showWidgetField, updateSpeed }) => {
const { state: geocodingState } = useContext(GeocodingContext);
const { dispatch: weatherDispatch } = useContext(WeatherContext);
const fetchCb = useCallback(() => {
if (geocodingState.coords) getWeatherInfo(weatherDispatch)(appid, geocodingState.coords.lon, geocodingState.coords.lat);
}, [geocodingState.coords]);
useEffect(() => {
let interval: NodeJS.Timeout | null = null;
if (!interval) {
interval = setInterval(fetchCb, updateSpeed);
} else {
if (!geocodingState.coords) clearInterval(interval);
}
return () => {
if (interval) clearInterval(interval);
}
}, [geocodingState.coords]);
return (
<>
</>
);
};
Unfortunately, the interval does not get cleared when the geocodingState.coords is nullified nor in the useEffect return.