From d015c4f1b92763bcf8b776a10ab17e71691670a3 Mon Sep 17 00:00:00 2001 From: evlist Date: Sun, 4 Sep 2022 21:08:31 +0200 Subject: [PATCH] Working around the fact that react's strict mode initializes compenents twice... --- src/components/live-map.tsx | 16 +++++++++++----- src/store/gpx.ts | 3 +++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/components/live-map.tsx b/src/components/live-map.tsx index ecce04e..3de6525 100644 --- a/src/components/live-map.tsx +++ b/src/components/live-map.tsx @@ -1,12 +1,11 @@ -import react, { useEffect } from 'react'; -import { useState } from 'react'; +import react, { useState } from 'react'; import { Map, Marker, ZoomControl } from 'pigeon-maps'; -import GPXOverlay from './gpx-overlay'; import { registerPlugin } from '@capacitor/core'; import { BackgroundGeolocationPlugin } from '@capacitor-community/background-geolocation'; import { useDispatch } from 'react-redux'; import { gpxActions } from '../store/gpx'; +import GPXOverlay from './gpx-overlay'; const BackgroundGeolocation = registerPlugin( 'BackgroundGeolocation' @@ -15,13 +14,19 @@ const BackgroundGeolocation = registerPlugin( const LiveMap: react.FC = () => { const initialCenter: [number, number] = [43.57154, 3.94351]; const initialZoom: number = 15; + const [initialized, setInitialized] = useState(false); const [center, setCenter] = useState(initialCenter); const [position, setPosition] = useState(initialCenter); const [zoom, setZoom] = useState(initialZoom); const dispatch = useDispatch(); - useEffect(() => { + if (!initialized) { + dispatch( + gpxActions.clearGPX({ + trkKey: 'current', + }) + ); dispatch( gpxActions.appendTrkpt({ trkKey: 'current', @@ -135,7 +140,8 @@ const LiveMap: react.FC = () => { .catch((reason) => { console.error('com.dyomedea.dyomedea LOG', ' - reason: ', reason); }); - }, [dispatch]); + setInitialized(true); + } return (