diff --git a/bs3/src/App.js b/bs3/src/App.js index b4ba0ba..60cb16f 100644 --- a/bs3/src/App.js +++ b/bs3/src/App.js @@ -4,12 +4,18 @@ import { Outlet } from 'react-router-dom'; import { useDispatch } from 'react-redux'; import { setLocation } from './app/geolocatorSlice'; import useGeoLocationCheck from './api/geolocation'; +import { useEffect } from 'react'; function App() { - const dispatch = useDispatch(); - const geoLocation = useGeoLocationCheck(); - dispatch(setLocation(geoLocation)); + const geoLocation = useGeoLocationCheck(); // Custom hook + + // Use useEffect to dispatch the action only when geoLocation is valid + useEffect(() => { + if (geoLocation) { + dispatch(setLocation(geoLocation)); + } + }, [dispatch, geoLocation]); return (
diff --git a/bs3/src/app/geolocatorSlice.js b/bs3/src/app/geolocatorSlice.js index 09dd630..4a259b2 100644 --- a/bs3/src/app/geolocatorSlice.js +++ b/bs3/src/app/geolocatorSlice.js @@ -3,7 +3,11 @@ import { createSlice } from '@reduxjs/toolkit'; export const geolocationSlice = createSlice({ name: 'geolocation', initialState: { - location: null, + location: { + loaded: false, + coordinates: { lat: null, lng: null }, + local: false, + } }, reducers: { setLocation: (state, action) => {