Introducing a new way to review your PRs.

Read the announcement

visgl / react-google-maps

React components and hooks for the Google Maps JavaScript API

View on GitHub

19 documents · Updated Nov 2, 2026

docs / api-loading-and-provider

Google Maps API loading and provider lifecycle

APIProvider is the application boundary for the Google Maps JavaScript API. It accepts the API key and first-load settings, starts one shared API load, and gives React children a context with API state and library access.

Provider role

The provider keeps Google Maps setup outside individual map components. It renders its children while it loads the API, then updates them when the shared API state changes. Most consumers should use the public hooks rather than read the provider context directly.

Initial configuration

The first APIProvider render defines the API key and load settings such as version, language, region, referrer policy, and requested libraries. It also accepts usage channels, usage attribution control, App Check token fetching, and load callbacks. These settings describe the API session, so later provider instances must use the same values.

References: src/…/api-provider.tsx• L47-116 src/…/api-provider.tsx• L225-246

Runtime load ownership

The API loader uses module-level state because the Maps JavaScript API can load only once in a browser runtime. The first load records the serialized settings, starts the API loader, and requests the core and maps libraries plus any libraries named by the provider. Later providers observe the same load instead of starting a second one.

The provider warns when a later instance supplies different settings. The new settings do not replace the settings used by the existing API session.

References: src/…/api-provider.tsx• L118-136 src/…/api-provider.tsx• L277-363

Loading lifecycle

The provider reports a small state machine to React consumers. The normal path starts at NOT_LOADED, moves to LOADING, and ends at LOADED; a load error ends at FAILED.

The following diagram shows how provider configuration becomes shared API access.

flowchart LR
  A[APIProvider settings] --> B[Shared loader state]
  B --> C[Set Google Maps loader options]
  C --> D[Load core and maps libraries]
  D -->|success| E[LOADED context]
  D -->|error| F[FAILED context and onError]
  E --> G[React consumers]

Loading states

APILoadingStatus is the shared vocabulary for API readiness. useApiLoadingStatus() returns the current value, while useApiIsLoaded() returns true only for LOADED; consumers can use the child page API loading status and library hooks for the hook-level behavior.

StateMeaning in the provider lifecycle
NOT_LOADEDNo provider-owned API load has started.
LOADINGThe API and its required initial libraries are being requested.
LOADEDThe required initial libraries are ready for consumers.
FAILEDLoading or initial library import failed.
AUTH_FAILUREA separate authorization-failure state in the shared status model; map rendering treats it as an auth error.

References: src/…/api-loading-status.ts• L1-9 src/…/api-provider.tsx• L212-223 src/…/use-api-loading-status.ts• L1-7 src/…/use-api-is-loaded.ts• L1-10 src/…/index.tsx• L217-225

Load completion

The provider marks the API as loaded only after the required initial library imports resolve. It then calls onLoad, if supplied, and makes the loaded libraries available through context. If the API already installed google.maps.importLibrary, the provider adopts that loader and waits for the same required libraries before reporting LOADED.

When the API is ready, an optional fetchAppCheckToken function is attached to Google Maps settings. This lets later API requests obtain App Check tokens without changing the provider lifecycle.

References: src/…/api-provider.tsx• L324-340 src/…/api-provider.tsx• L352-367 src/…/api-provider.tsx• L385-393

Failure handling

Any error from the initial API or library load changes the provider state to FAILED. The provider calls onError when present. Without that callback, it writes the failure to the console. Consumers should use the loading status to avoid creating Google Maps objects before readiness.

References: src/…/api-provider.tsx• L368-379 src/…/use-api-loading-status.ts• L5-7

Consumer context

The provider supplies one context value to its descendants. The value combines API state and library import access with registries for active 2D and 3D map instances, so map components and hooks can share the same API session.

React context

APIProviderContext is the internal connection between the provider and library consumers. The provider publishes status, loaded libraries, and an import function together with named map and 3D map instance access. Applications normally reach these capabilities through public hooks and components rather than through the context object.

References: src/…/api-provider.tsx• L21-45 src/…/api-provider.tsx• L432-467

Library access

The provider exposes a cached import function for optional Google Maps libraries. A request imports a library once, stores the resolved library in context, and returns the cached value for later requests. The child page API loading status and library hooks explains how useMapsLibrary() turns this access into a React-friendly loading pattern.

References: src/…/api-provider.tsx• L248-262 src/…/api-provider.tsx• L395-399 src/…/use-maps-library.ts• L8-28

Map instance access

The same context tracks map instances by name. A default name supports the common single-map case, while explicit names let an application keep several map instances available to hooks and related components. The provider also keeps a separate registry for 3D map elements.

References: src/…/api-provider.tsx• L139-190 src/…/api-provider.tsx• L21-35

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the one-load rule aligned with the serialized option check and with behavior for multiple React roots.
  • Keep APILoadingStatus values aligned with provider transitions and with consumers that render authorization failures.
  • Keep the initial library set aligned with the point at which LOADED is published and onLoad runs.
  • Keep the context contract aligned with the loading hooks, library hook, and map instance access paths.
  • Keep externally installed google.maps.importLibrary behavior aligned with option setup and readiness reporting.

Important failure modes

TriggerConsequenceGuard
A later provider uses different API settingsThe new settings are ignored and a warning is logged.Use one provider when possible and set final values on its first render.
The API or a required initial library rejectsStatus becomes FAILED; onError runs or the error is logged.Render API-dependent behavior from the loading status.
A library is requested before the API is loadedThe library hook does not start the request yet.Wait for LOADED through the public loading hooks.
A provider is missing above a consumerThe loading hook falls back to NOT_LOADED; context-dependent map access cannot resolve a provider.Place API-dependent components below APIProvider.

Key files

FileWhy to read it
src/…/api-provider.tsxDefines provider configuration, shared load ownership, context values, and failure callbacks.
src/…/api-loading-status.tsDefines the status values shared by the provider and consumers.
src/…/use-api-loading-status.tsShows the public status read from provider context.
src/…/use-api-is-loaded.tsShows the simple readiness check used by API-dependent consumers.
src/…/use-maps-library.tsShows how optional libraries are requested after API readiness.
src/…/index.tsxShows how a map responds to the authorization-failure status.