1
1
# ` <APIProvider> ` Component
2
2
3
3
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.
4
31
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
+ :::
10
33
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
16
35
17
36
The ` APIProvider ` only needs the [ Google Maps API Key] [ gmp-api-keys ] to function.
18
37
This has to be provided via the ` apiKey ` prop:
19
38
20
- ``` tsx title="app.jsx"
39
+ ``` tsx
21
40
import React from ' react' ;
22
41
import {APIProvider } from ' @vis.gl/react-google-maps' ;
23
42
@@ -34,9 +53,13 @@ export default App;
34
53
The props are based on [ the parameters] [ gmp-params ] available for the
35
54
'Dynamic Library Import' API which we are using under the hood.
36
55
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
+ :::
40
63
41
64
### Required
42
65
@@ -96,8 +119,8 @@ map-instances, libraries and the loading-status.
96
119
97
120
Client code should never need to interact with the context directly, always
98
121
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.
101
124
102
125
:::
103
126
@@ -112,10 +135,12 @@ The following hooks are built to work with the `APIProvider` Component:
112
135
113
136
[ ` src/components/api-provider.tsx ` ] [ api-provider-src ] .
114
137
138
+ [ gmp-import-library ] : https://developers.google.com/maps/documentation/javascript/load-maps-js-api#dynamic-library-import
115
139
[ gmp-api-keys ] : https://developers.google.com/maps/documentation/javascript/get-api-key
116
140
[ gmp-params ] : https://developers.google.com/maps/documentation/javascript/load-maps-js-api#required_parameters
117
141
[ gmp-api-version ] : https://developers.google.com/maps/documentation/javascript/versions
118
142
[ gmp-libs ] : https://developers.google.com/maps/documentation/javascript/libraries
119
143
[ gmp-region ] : https://developers.google.com/maps/documentation/javascript/localization#Region
120
144
[ gmp-lang ] : https://developers.google.com/maps/documentation/javascript/localization
121
145
[ 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