Working around the fact that react's strict mode initializes compenents twice...

This commit is contained in:
Eric van der Vlist 2022-09-04 21:08:31 +02:00
parent 5867338836
commit d015c4f1b9
2 changed files with 14 additions and 5 deletions

View File

@ -1,12 +1,11 @@
import react, { useEffect } from 'react'; import react, { useState } from 'react';
import { useState } from 'react';
import { Map, Marker, ZoomControl } from 'pigeon-maps'; import { Map, Marker, ZoomControl } from 'pigeon-maps';
import GPXOverlay from './gpx-overlay';
import { registerPlugin } from '@capacitor/core'; import { registerPlugin } from '@capacitor/core';
import { BackgroundGeolocationPlugin } from '@capacitor-community/background-geolocation'; import { BackgroundGeolocationPlugin } from '@capacitor-community/background-geolocation';
import { useDispatch } from 'react-redux'; import { useDispatch } from 'react-redux';
import { gpxActions } from '../store/gpx'; import { gpxActions } from '../store/gpx';
import GPXOverlay from './gpx-overlay';
const BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>( const BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>(
'BackgroundGeolocation' 'BackgroundGeolocation'
@ -15,13 +14,19 @@ const BackgroundGeolocation = registerPlugin<BackgroundGeolocationPlugin>(
const LiveMap: react.FC = () => { const LiveMap: react.FC = () => {
const initialCenter: [number, number] = [43.57154, 3.94351]; const initialCenter: [number, number] = [43.57154, 3.94351];
const initialZoom: number = 15; const initialZoom: number = 15;
const [initialized, setInitialized] = useState(false);
const [center, setCenter] = useState(initialCenter); const [center, setCenter] = useState(initialCenter);
const [position, setPosition] = useState(initialCenter); const [position, setPosition] = useState(initialCenter);
const [zoom, setZoom] = useState(initialZoom); const [zoom, setZoom] = useState(initialZoom);
const dispatch = useDispatch(); const dispatch = useDispatch();
useEffect(() => { if (!initialized) {
dispatch(
gpxActions.clearGPX({
trkKey: 'current',
})
);
dispatch( dispatch(
gpxActions.appendTrkpt({ gpxActions.appendTrkpt({
trkKey: 'current', trkKey: 'current',
@ -135,7 +140,8 @@ const LiveMap: react.FC = () => {
.catch((reason) => { .catch((reason) => {
console.error('com.dyomedea.dyomedea LOG', ' - reason: ', reason); console.error('com.dyomedea.dyomedea LOG', ' - reason: ', reason);
}); });
}, [dispatch]); setInitialized(true);
}
return ( return (
<Map <Map

View File

@ -20,6 +20,9 @@ const slice = createSlice({
action.payload.trkpt action.payload.trkpt
); );
}, },
clearGPX(state, action) {
state[action.payload.trkKey] = initialState.current;
},
}, },
}); });