Skip to content

Commit 3a1e058

Browse files
committed
hook-example: Explain how useMap in code comments
- Show how useMap works on first render, second render - Hint at useMap not working outside Add console statements and code comments to reference how useMap works in the different situations. The code comment help understand that useMap returns undefined first; and what the shape of the returned object looks like. The controls2 component shows how the "current" works when useMap is used inside a Map component. All console logs are formatted as code comments so they are easy to reproduce but don't actually log.
1 parent fdf58b2 commit 3a1e058

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

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: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,33 @@ 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 demo = useMap();
11+
* console.log('Controls', {demo});
12+
* ```
13+
* ### First render:
14+
* ```
15+
* {
16+
* "demo": {
17+
* "current": undefined
18+
* }
19+
* }
20+
* ```
21+
* ### Second render:
22+
* ```
23+
* {
24+
* "demo": {
25+
* "current": undefined,
26+
* "mymap": {...} // See https://visgl.github.io/react-map-gl/docs/api-reference/types#mapref
27+
* }
28+
* }
29+
* ```
30+
*/
31+
32+
const {mymap} = useMap(); // `mymap` is the id in <Map id="mymap" />
33+
834
const [inputValue, setInputValue] = useState('');
935
const [hasError, setError] = useState(false);
1036

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 demo = useMap();
12+
* console.log('Controls2', {demo});
13+
* ```
14+
* ### First render:
15+
* ```
16+
* {
17+
* "demo": {
18+
* "current": {...},
19+
* "mymap": {...}
20+
* }
21+
* }
22+
* ```
23+
*/
24+
25+
return null;
26+
}

examples/get-started/hook/map.jsx

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

44
import 'mapbox-gl/dist/mapbox-gl.css';
5+
import Controls2 from './controls2';
56

67
const MAPBOX_TOKEN = ''; // Set your mapbox token here
78

89
export default function MapView() {
10+
/**
11+
* ## This is how `useMap` works:
12+
* ```
13+
* const demo = useMap();
14+
* console.log('MapView', {demo});
15+
* ```
16+
* ### First render:
17+
* ```
18+
* {
19+
* "demo": {
20+
* "current": undefined
21+
* }
22+
* }
23+
* Second render:
24+
* {
25+
* "demo": {
26+
* "current": undefined,
27+
* "mymap": {...} // See https://visgl.github.io/react-map-gl/docs/api-reference/types#mapref
28+
* }
29+
* }
30+
* ```
31+
*/
32+
933
return (
1034
<Map
11-
id="mymap"
35+
id="mymap" // relevant for `useMap`, see control.js, controls2.js
1236
initialViewState={{
1337
longitude: -122.4,
1438
latitude: 37.8,
@@ -17,6 +41,8 @@ export default function MapView() {
1741
style={{width: 800, height: 600}}
1842
mapStyle="mapbox://styles/mapbox/streets-v9"
1943
mapboxAccessToken={MAPBOX_TOKEN}
20-
/>
44+
>
45+
<Controls2 />
46+
</Map>
2147
);
2248
}

0 commit comments

Comments
 (0)