Skip to content

Commit ad56dd6

Browse files
authored
Improve docs and hooks-example for useMap and MapProvider (#2118)
1 parent dac4874 commit ad56dd6

File tree

6 files changed

+91
-20
lines changed

6 files changed

+91
-20
lines changed

docs/api-reference/map.md

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ function App() {
5353
</TabItem>
5454
</Tabs>
5555

56-
5756
## Properties
5857

5958
Aside from the props listed below, the `Map` component supports all parameters of the `Map` class constructor ([Mapbox](https://docs.mapbox.com/mapbox-gl-js/api/map/) | [Maplibre](https://maplibre.org/maplibre-gl-js/docs/API/type-aliases/MapOptions/)). Beware that this is not an exhaustive list of all props. Different base map libraries may offer different options and default values. When in doubt, refer to your base map library's documentation.
@@ -64,6 +63,10 @@ Aside from the props listed below, the `Map` component supports all parameters o
6463

6564
Map container id.
6665

66+
Required when [`MapProvider`](./map-provider.md)s are used. Used to reference the map with [`useMap`](./use-map.md).
67+
68+
Make sure to pick a name that has no conflict with other imports (there are no checks or errors in this case).
69+
6770
#### `style`: CSSProperties {#style}
6871

6972
Default: `{position: 'relative', width: '100%', height: '100%'}`
@@ -116,7 +119,6 @@ Enable diffing when `mapStyle` changes. If `false`, force a 'full' update, remov
116119
Terrain property of the style. Must conform to the [Terrain Style Specification](https://docs.mapbox.com/mapbox-gl-js/style-spec/terrain/).
117120
If `undefined` is provided, removes terrain from the map.
118121

119-
120122
### Camera options
121123

122124
#### `initialViewState`: object {#initialviewstate}
@@ -461,13 +463,14 @@ Called when one of the map's sources loads or changes, including if a tile belon
461463

462464
### Other options
463465

464-
The following props, along with any options of the `Map` class ([Mapbox](https://docs.mapbox.com/mapbox-gl-js/api/map/) | [Maplibre](https://maplibre.org/maplibre-gl-js/docs/API/type-aliases/MapOptions/)) not listed above, can be specified to construct the underlying `Map` instance.
466+
The following props, along with any options of the `Map` class ([Mapbox](https://docs.mapbox.com/mapbox-gl-js/api/map/) | [Maplibre](https://maplibre.org/maplibre-gl-js/docs/API/type-aliases/MapOptions/)) not listed above, can be specified to construct the underlying `Map` instance.
465467

466468
Note: props in this section are not reactive. They are only used once when the Map instance is constructed.
467469

468470
#### `mapLib`: any {#maplib}
469471

470472
Default:
473+
471474
- `import('mapbox-gl')` if imported from `react-map-gl`
472475
- `import('maplibre-gl')` if imported from `react-map-gl/maplibre`
473476

@@ -499,7 +502,7 @@ function App() {
499502
Or to load a pre-bundled version of the library:
500503

501504
```html title="index.html"
502-
<script src="https://api.mapbox.com/mapbox-gl-js/v2.4.0/mapbox-gl.js" ></script>
505+
<script src="https://api.mapbox.com/mapbox-gl-js/v2.4.0/mapbox-gl.js"></script>
503506
```
504507

505508
```tsx title="app.tsx"
@@ -511,7 +514,6 @@ function App() {
511514
}
512515
```
513516

514-
515517
#### `mapboxAccessToken`: string {#mapboxaccesstoken}
516518

517519
Token used to access the Mapbox data service. See [about map tokens](../get-started/mapbox-tokens.md).
@@ -561,8 +563,6 @@ The number of web workers instantiated on a page with mapbox-gl maps.
561563

562564
Provides an interface for loading mapbox-gl's WebWorker bundle from a self-hosted URL. This is useful if your site needs to operate in a strict CSP (Content Security Policy) environment wherein you are not allowed to load JavaScript code from a Blob URL, which is default behavior.
563565

564-
565-
566566
## Methods
567567

568568
Imperative methods are accessible via a [React ref](https://reactjs.org/docs/refs-and-the-dom.html#creating-refs) or the [useMap](./use-map.md) hook.
@@ -592,7 +592,6 @@ function App() {
592592
</TabItem>
593593
<TabItem value="maplibre" label="Maplibre">
594594

595-
596595
```tsx
597596
import * as React from 'react';
598597
import {useRef, useCallback} from 'react';
@@ -612,7 +611,6 @@ function App() {
612611
}
613612
```
614613

615-
616614
</TabItem>
617615
</Tabs>
618616

@@ -624,8 +622,6 @@ You can still access the hidden members via `getMap()`:
624622

625623
Returns the native `Map` ([Mapbox](https://docs.mapbox.com/mapbox-gl-js/api/map/) | [Maplibre](https://maplibre.org/maplibre-gl-js/docs/API/classes/Map/)) instance associated with this component.
626624

627-
628-
629625
## Source
630626

631627
[map.tsx](https://github.com/visgl/react-map-gl/tree/7.0-release/src/components/map.tsx)

docs/api-reference/use-map.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# useMap
22

3-
The `useMap` hook allows a custom component to reference the [Map](./map.md) that contains it.
3+
The `useMap` hook allows a component to reference the [Map](./map.md) that contains it.
4+
5+
When used with [MapProvider](./map-provider.md), this hook can also reference maps that are rendered outside of the current map component's direct render tree.
46

57
import Tabs from '@theme/Tabs';
68
import TabItem from '@theme/TabItem';
@@ -60,7 +62,7 @@ function NavigateButton() {
6062
</Tabs>
6163

6264

63-
When used with the [MapProvider](./map-provider.md), this hook can also reference maps that are rendered outside of the current component's direct render tree.
65+
When used with the [MapProvider](./map-provider.md), this hook can also reference maps that are rendered outside of the current component's direct render tree as long as both trees are part of the current `<MapProvider>`.
6466

6567

6668
<Tabs groupId="map-library">
@@ -70,6 +72,8 @@ When used with the [MapProvider](./map-provider.md), this hook can also referenc
7072
import {MapProvider, Map, useMap} from 'react-map-gl';
7173

7274
function Root() {
75+
// Note: `useMap` will not work in <Root>, only children of <MapProvider> can use `useMap`
76+
7377
return (
7478
<MapProvider>
7579
<Map id="myMapA" ... />
@@ -131,7 +135,7 @@ See a full example [here](https://github.com/visgl/react-map-gl/tree/7.0-release
131135

132136
The hook returns an object that contains all mounted maps under the closest `MapProvider`. The keys are each map's [id](./map.md#id) and the values are the [MapRef](./types.md#mapref).
133137

134-
If the hook is used inside a decendent of a `Map` component, the returned object also contains a `current` field that references the containing map.
138+
If the hook is used inside a decendent of a `Map` component, the returned object additionally contains a `current` field that references the containing map.
135139

136140
## Source
137141

examples/get-started/hook/app.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Map from './map';
77
import Controls from './controls';
88

99
function Root() {
10+
// Note: `useMap` will not work here, only child components of `MapProvider` or `Map` can use `useMap`
11+
1012
return (
1113
<MapProvider>
1214
<Controls />

examples/get-started/hook/controls.jsx

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,34 @@ import {useCallback, useState, useEffect} from 'react';
44
import {useMap} from 'react-map-gl';
55

66
export default function Controls() {
7-
const {mymap} = useMap();
7+
/**
8+
* ## This is how `useMap` works:
9+
* ```
10+
* const maps = useMap();
11+
* console.log('Controls useMap()', maps);
12+
* ```
13+
* ### First render:
14+
* ```
15+
* {
16+
* "current": undefined
17+
* }
18+
* ```
19+
* ### Second render:
20+
* ```
21+
* {
22+
* "current": undefined,
23+
* "mymap": {...} // See https://visgl.github.io/react-map-gl/docs/api-reference/types#mapref
24+
* }
25+
* ```
26+
*/
27+
28+
const {mymap} = useMap(); // `mymap` is the id in <Map id="mymap" />
29+
830
const [inputValue, setInputValue] = useState('');
931
const [hasError, setError] = useState(false);
1032

1133
useEffect(() => {
12-
if (!mymap) {
13-
return undefined;
14-
}
34+
if (!mymap) return undefined;
1535

1636
const onMove = () => {
1737
const {lng, lat} = mymap.getCenter();
@@ -31,6 +51,8 @@ export default function Controls() {
3151
}, []);
3252

3353
const onSubmit = useCallback(() => {
54+
if (!mymap) return;
55+
3456
const [lng, lat] = inputValue.split(',').map(Number);
3557
if (Math.abs(lng) <= 180 && Math.abs(lat) <= 85) {
3658
mymap.easeTo({
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// import {useMap} from 'react-map-gl';
2+
3+
export default function Controls2() {
4+
/**
5+
* ## This is how `useMap` works:
6+
* This component does nothing. It's purpose is to demo `useMap`.
7+
* When a component is a child of `<Map>`, `useMap` has a `current` field that references the containing map.
8+
* See https://visgl.github.io/react-map-gl/docs/api-reference/use-map
9+
* See https://visgl.github.io/react-map-gl/docs/api-reference/types#mapref
10+
* ```
11+
* const maps = useMap();
12+
* console.log('Controls2 useMap()', maps);
13+
* ```
14+
* ### First render:
15+
* ```
16+
* {
17+
* "current": {...}, // this is the same as `mymap`
18+
* "mymap": {...}
19+
* }
20+
* ```
21+
*/
22+
23+
return null;
24+
}

examples/get-started/hook/map.jsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
import * as React from 'react';
22
import Map from 'react-map-gl';
3+
// import {useMap} from 'react-map-gl';
34

45
import 'mapbox-gl/dist/mapbox-gl.css';
6+
import Controls2 from './controls2';
57

68
const MAPBOX_TOKEN = ''; // Set your mapbox token here
79

810
export default function MapView() {
11+
/**
12+
* ## This is how `useMap` works:
13+
* ```
14+
* const maps = useMap();
15+
* console.log('MapView useMap()', maps);
16+
* ```
17+
* ### First render:
18+
* ```
19+
* {
20+
* "current": undefined
21+
* }
22+
* Second render:
23+
* {
24+
* "current": undefined,
25+
* "mymap": {...} // See https://visgl.github.io/react-map-gl/docs/api-reference/types#mapref
26+
* }
27+
* ```
28+
*/
29+
930
return (
1031
<Map
11-
id="mymap"
32+
id="mymap" // relevant for `useMap`, see control.js, controls2.js
1233
initialViewState={{
1334
longitude: -122.4,
1435
latitude: 37.8,
@@ -17,6 +38,8 @@ export default function MapView() {
1738
style={{width: 800, height: 600}}
1839
mapStyle="mapbox://styles/mapbox/streets-v9"
1940
mapboxAccessToken={MAPBOX_TOKEN}
20-
/>
41+
>
42+
<Controls2 />
43+
</Map>
2144
);
2245
}

0 commit comments

Comments
 (0)