Skip to content

Commit

Permalink
update (un-)mount example
Browse files Browse the repository at this point in the history
  • Loading branch information
g4rb4g3 committed Jan 7, 2025
1 parent 8e3c897 commit e6cd84e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 21 deletions.
51 changes: 51 additions & 0 deletions example/src/examples/Map/MapUnMount.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React, { useState, useEffect } from 'react';
import Mapbox from '@rnmapbox/maps';
import { Button } from '@rneui/base';

import sheet from '../../styles/sheet';
import { ExampleWithMetadata } from '../common/ExampleMetadata'; // exclude-from-doc

const MapUnMount = () => {
const [isMounted, setIsMounted] = useState(true);

useEffect(() => {
Mapbox.locationManager.start();

return (): void => {
Mapbox.locationManager.stop();
};
}, []);

return (
<>
<Button
onPress={() => setIsMounted((mounted) => !mounted)}
title={isMounted ? 'unmount MapView' : 'mount MapView'}
/>
{isMounted ? (
<Mapbox.MapView
styleURL={Mapbox.StyleURL.Dark}
style={sheet.matchParent}
testID={'show-map'}
>
<Mapbox.Camera followZoomLevel={12} followUserLocation />

<Mapbox.UserLocation />
</Mapbox.MapView>
) : null}
</>
);
};

export default MapUnMount;

/* end-example-doc */

const metadata: ExampleWithMetadata['metadata'] = {
title: 'Map (un-)mount',
tags: [],
docs: `
Showing and hiding the the map should not lead to increased memory consumption, use this example to check it on the profiler.
`,
};
MapUnMount.metadata = metadata;
31 changes: 10 additions & 21 deletions example/src/examples/Map/ShowMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ const ShowMap = () => {
data: (Mapbox.StyleURL as any)[key], // bad any, because enums
};
})
.sort(onSortOptions)
.concat({ data: 'mount', label: '(un)mount' });
.sort(onSortOptions);

const [styleURL, setStyleURL] = useState({ styleURL: _mapOptions[0].data });
const [isMounted, setIsMounted] = useState(true);

useEffect(() => {
Mapbox.locationManager.start();
Expand All @@ -44,26 +42,17 @@ const ShowMap = () => {
selectedIndex={_mapOptions.findIndex(
(i) => i.data === styleURL.styleURL,
)}
onPress={(index) => {
const { data } = _mapOptions[index];
if (data === 'mount') {
setIsMounted((m) => !m);
return;
}
onMapChange(index, data);
}}
onPress={(index) => onMapChange(index, _mapOptions[index].data)}
/>
{isMounted ? (
<Mapbox.MapView
styleURL={styleURL.styleURL}
style={sheet.matchParent}
testID={'show-map'}
>
<Mapbox.Camera followZoomLevel={12} followUserLocation />
<Mapbox.MapView
styleURL={styleURL.styleURL}
style={sheet.matchParent}
testID={'show-map'}
>
<Mapbox.Camera followZoomLevel={12} followUserLocation />

<Mapbox.UserLocation onPress={onUserMarkerPress} />
</Mapbox.MapView>
) : null}
<Mapbox.UserLocation onPress={onUserMarkerPress} />
</Mapbox.MapView>
</>
);
};
Expand Down
1 change: 1 addition & 0 deletions example/src/examples/Map/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export { default as PointInMapView } from './PointInMapView';
export { default as ShowAndHideLayer } from './ShowAndHideLayer';
export { default as ShowClick } from './ShowClick';
export { default as ShowMap } from './ShowMap';
export { default as MapUnMount } from './MapUnMount';
export { default as ShowMapLocalStyle } from './ShowMapLocalStyle';
export { default as ShowRegionDidChange } from './ShowRegionDidChange';
export { default as SourceLayerVisibility } from './SourceLayerVisibility';
Expand Down

0 comments on commit e6cd84e

Please sign in to comment.