Skip to content

Commit adba86a

Browse files
committed
docs: update api-provider docs
1 parent 743878a commit adba86a

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

docs/api-reference/components/api-provider.md

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,42 @@
11
# `<APIProvider>` Component
22

33
The `APIProvider` is our component to load the Google Maps JavaScript API.
4+
Besides that, it provides context information and functions for the other
5+
components and hooks of this library.
6+
7+
It can be added at any level of the application (typically somewhere
8+
at the top of your component-tree), and it will render all child components
9+
unmodified. A re-render is only triggered once the loading status of the
10+
Google Maps API changes.
11+
12+
Normally, there should only be a single instance of the APIProvider in a page,
13+
but there are situations (e.g., multiple React render-roots in a page) where
14+
this isn't possible. In those cases, make sure to create all `APIProvider`
15+
components using the exact same props, since only the first one to
16+
render will actually load the maps API.
17+
18+
When the Google Maps API has already been loaded externally
19+
(i.e., the [`google.maps.importLibrary`][gmp-import-library] function exists),
20+
the
21+
`APIProvider` will ignore all specified props and use the existing
22+
`importLibrary` function.
23+
24+
:::info[Important]
25+
26+
The Maps JavaScript API can only be loaded once, and settings like the
27+
language and region cannot be changed after loading. Therefore, it is
28+
important to make sure the props are specified with their final values when
29+
the `APIProvider` component is first rendered. Changing these props after the
30+
first render will in most cases have no effect, cause an error, or both.
431

5-
This component can be added at any level of the application (typically somewhere
6-
at the top), and it will render all child components unmodified.
7-
8-
Besides handling the loading of the Maps JavaScript API, it also provides context
9-
information and functions for the other components and hooks of this library.
32+
:::
1033

11-
> **Please Note:** since the Maps JavaScript API can only be loaded once, and
12-
> settings like the language and region cannot be changed after loading, it is
13-
> important to make sure the props are specified with their final values when
14-
> the APIProvider component is rendered. Changing these props after the first
15-
> render will in most cases have no effect, cause an error, or both.
34+
## Usage
1635

1736
The `APIProvider` only needs the [Google Maps API Key][gmp-api-keys] to function.
1837
This has to be provided via the `apiKey` prop:
1938

20-
```tsx title="app.jsx"
39+
```tsx
2140
import React from 'react';
2241
import {APIProvider} from '@vis.gl/react-google-maps';
2342

@@ -34,9 +53,13 @@ export default App;
3453
The props are based on [the parameters][gmp-params] available for the
3554
'Dynamic Library Import' API which we are using under the hood.
3655

37-
> Note that most of the props below are marked with 'first-render only',
38-
> which refers to the fact that these can only be specified on
39-
> first render, later changes to the values will have no effect.
56+
:::note
57+
58+
Most of the props below are marked with 'first-render only',
59+
which refers to the fact that these can only be specified on
60+
first render, later changes to the values will have no effect.
61+
62+
:::
4063

4164
### Required
4265

@@ -96,8 +119,8 @@ map-instances, libraries and the loading-status.
96119

97120
Client code should never need to interact with the context directly, always
98121
use the corresponding hooks instead.
99-
If you feel like you need to directly access the context, please file a
100-
bug report about this.
122+
If you feel like you need to directly access the context, please [file a
123+
bug report or feature request][rgm-new-issue] about this.
101124

102125
:::
103126

@@ -112,10 +135,12 @@ The following hooks are built to work with the `APIProvider` Component:
112135

113136
[`src/components/api-provider.tsx`][api-provider-src].
114137

138+
[gmp-import-library]: https://developers.google.com/maps/documentation/javascript/load-maps-js-api#dynamic-library-import
115139
[gmp-api-keys]: https://developers.google.com/maps/documentation/javascript/get-api-key
116140
[gmp-params]: https://developers.google.com/maps/documentation/javascript/load-maps-js-api#required_parameters
117141
[gmp-api-version]: https://developers.google.com/maps/documentation/javascript/versions
118142
[gmp-libs]: https://developers.google.com/maps/documentation/javascript/libraries
119143
[gmp-region]: https://developers.google.com/maps/documentation/javascript/localization#Region
120144
[gmp-lang]: https://developers.google.com/maps/documentation/javascript/localization
121145
[api-provider-src]: https://github.com/visgl/react-google-maps/blob/main/src/components/api-provider.tsx
146+
[rgm-new-issue]: https://github.com/visgl/react-google-maps/issues/new/choose

0 commit comments

Comments
 (0)