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-hooks

API loading status and library hooks

The loading hooks give React components a safe view of Google Maps API readiness and optional library access. They depend on the APIProvider, which owns the shared load state and makes library results available to its descendants.

API readiness

Loading status

useApiLoadingStatus reports the shared API state as NOT_LOADED, LOADING, LOADED, FAILED, or AUTH_FAILURE. A component can use this status to choose whether to show a loading view, enable Maps features, or report a load problem.

The hook reads the provider state. Without an APIProvider above the component, it reports NOT_LOADED, so the absence of provider context does not look like a ready API.

References: src/…/use-api-loading-status.ts• L1-7 src/…/api-loading-status.ts• L1-9 src/…/api-provider.tsx• L21-45

Loaded flag

useApiIsLoaded provides the narrower question: whether the API has finished loading successfully. It is true only for LOADED; all other states remain false.

Use this flag when a component only needs to guard access to the Maps API. Use useApiLoadingStatus when the component must distinguish loading from failure.

References: src/…/use-api-is-loaded.ts• L1-10 src/…/api-loading.test.tsx• L46-62

Optional library access

Library retrieval

useMapsLibrary obtains a named Google Maps library through the provider and returns the library object when it is ready. Before that point, it returns null, which lets a component render without using classes that are not available yet.

The hook supports both libraries requested up front through the provider and libraries requested later by a component. This keeps the main API load separate from optional features such as Places, Geometry, Marker, or Maps 3D support.

References: src/…/use-maps-library.ts• L6-14 src/…/use-maps-library.ts• L25-29 src/…/api-provider.tsx• L197-223

Library loading lifecycle

The hook waits until the main API is ready before it asks the provider to import the named library. The provider stores the result and publishes it through context, so the hook changes from null to the library object and the consuming component can then create Maps objects.

The request is tied to the library name and the current provider. A component should treat the returned value as a readiness signal and use it in its effect or render guard.

This diagram shows the normal path from provider state to optional library use.

flowchart LR
  A[APIProvider] --> B{API loaded?}
  B -- No --> C[useMapsLibrary returns null]
  B -- Yes --> D[Provider imports named library]
  D --> E{Library ready?}
  E -- No --> C
  E -- Yes --> F[Hook returns library object]
  F --> G[Component uses library classes]

References: src/…/use-maps-library.ts• L13-27 src/…/api-provider.tsx• L248-262 src/…/api-provider.tsx• L295-363

Provider context

Context requirement

All three hooks are consumer views of APIProviderContext. Components must render below an APIProvider to receive live status changes or to request optional libraries.

Outside that context, readiness hooks stay in their safe not-ready state and useMapsLibrary returns null without starting a library request. The hooks do not create a separate API loader for an unwrapped component.

References: src/…/api-provider.tsx• L44-45 src/…/use-api-loading-status.ts• L5-7 src/…/use-maps-library.ts• L13-23

Shared lifecycle

The API load is shared at runtime. Provider instances observe the same loading status, and the provider calls a library import only after the API can accept it. If the API was loaded by another source, the provider uses the existing import entry point and still exposes its status and library results to descendants.

Consumers should not assume that changing provider load settings restarts the API. The Maps API is loaded once, so the provider's first effective settings define the shared load.

References: src/…/api-provider.tsx• L118-135 src/…/api-provider.tsx• L264-275 src/…/api-provider.tsx• L324-340

  • Google Maps API loading and provider lifecycle explains how APIProvider configures and starts the shared API load.
  • Hook reference: useApiLoadingStatus documents the status hook API.
  • Hook reference: useApiIsLoaded documents the boolean readiness hook.
  • Hook reference: useMapsLibrary documents optional library access.

Change checklist

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

  • Keep the status values and the readiness rule aligned. useApiIsLoaded must remain true only for the successful loaded state.
  • Keep provider context behavior aligned across all three hooks. A missing provider must not start a second loader or report readiness.
  • When library loading changes, check the contract that a missing library is represented by null until the provider publishes the loaded result.
  • When provider load timing changes, check consumers that use the API readiness hook before requesting an optional library.

Important failure modes

TriggerConsequenceGuard
A consumer renders outside APIProviderStatus stays NOT_LOADED and library access stays nullPlace the consumer below the provider in the React tree.
A component uses a library before useMapsLibrary returns itMaps classes or services are unavailableGuard work on a non-null library value.
The API load fails or has an auth failureuseApiIsLoaded remains false and dependent features must not startRead useApiLoadingStatus when the UI must show or handle the failure.
Provider settings change after the shared API load startsNew settings can be ignored or conflict with the existing loadSet final API settings on the first provider render.

Key files

FileWhy to read it
src/…/use-api-loading-status.tsDefines the provider-backed status view and its safe default.
src/…/use-api-is-loaded.tsDefines the successful-readiness rule used by consumers.
src/…/use-maps-library.tsDefines lazy optional-library access and its null-until-ready contract.
src/…/api-loading-status.tsDefines the shared API status values.
src/…/api-provider.tsxShows the context contract, shared load lifecycle, and library result publication.
src/…/api-loading.test.tsxShows the expected readiness behavior for provider-backed consumers.