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 / react-integration-utilities

React integration utilities

The React integration utilities form the shared layer between React state and Google Maps objects. They keep callbacks current without needless listener work, decide when values changed, copy props to mutable map elements, and turn map or DOM events into safe React updates. The utilities support the map runtime and map overlays, while option updates have a separate guide in map-object option reconciliation.

Synchronization model

React renders describe desired values, but Maps objects and DOM elements keep their own mutable state. These utilities connect the two sides through stable references, effect-based updates, and event cleanup.

flowchart LR
  P[React props and callbacks] --> C[Comparison and callback utilities]
  C --> B[Prop bindings and event subscriptions]
  B --> M[Maps objects or DOM elements]
  M --> E[Map and DOM events]
  E --> U[Refs and React updates]
  U --> P

Stable callback delivery

useEffectEvent gives an installed listener a stable call path while keeping the callback it invokes current. This lets an event subscription depend on its target and event name instead of being rebuilt only because a component created a new callback during render. The utility also blocks calls during render and supports React versions that do not provide the native effect-event API. References: src/…/use-effect-event.ts• L9-17, src/…/use-effect-event.ts• L33-53, src/…/use-effect-event.ts• L56-63

Value comparison

useMemoized keeps the last value that is equal under a caller-provided comparison rule. useCustomCompareEffect uses that stable value as an effect dependency, and useDeepCompareEffect supplies deep comparison for nested option data. This prevents new object identities from causing work when the meaningful content is unchanged. References: src/…/use-memoized.ts• L6-16, src/…/use-custom-compare-efffect.ts• L4-10, src/…/use-deep-compare-effect.ts• L5-10, src/…/rectangle.tsx• L63-74

Property and event bridges

The bridge utilities apply React inputs to objects that React does not own. They also manage listener lifetime so a target receives events only while the related component is active.

Prop binding

usePropBinding copies a prop value to a writable Maps element after the element exists and when the value changes. It does not write an omitted value to a new object, because that could replace an API default or trigger a warning; after a value has been written, undefined can clear it. This rule is used by 3D map elements and overlay components such as Marker3D, Popover, and CirclePath. References: src/…/use-prop-binding.ts• L6-18, src/…/use-prop-binding.ts• L21-38, src/…/marker-3d.tsx• L201-214, src/…/popover.tsx• L132-144, src/…/circle-path.tsx• L17-24

Event subscriptions

useDomEventListener attaches a listener to a DOM node, while useMapsEventListener attaches one to a Maps JavaScript API object. Both skip work when the target, event name, or callback is absent and remove the exact subscription during cleanup. Their use of useEffectEvent keeps callback changes separate from subscription changes. References: src/…/use-dom-event-listener.ts• L7-29, src/…/use-maps-event-listener.ts• L7-25

Event shape adaptation

Components use the low-level event bridges for direct subscriptions, while map components add a higher-level event shape for application code. The map event layer maps React prop names to Maps event names and supplies normalized camera or pointer details. The 3D map event layer performs the same role for custom-element events and can combine several camera changes into one camera event. References: src/…/use-map-events.ts• L38-72, src/…/use-map-events.ts• L82-140, src/…/use-map-3d-events.ts• L151-200, src/…/use-map-3d-events.ts• L250-265

Render and instance coordination

Some integrations need a React ref to represent an external instance, or need a render after an external event changes that instance. These utilities provide that coordination without making the external object part of React state.

Callback refs

useCallbackRef turns a mounted DOM node or map element into state and returns a callback ref for attaching it. The state change tells the owning hook when the external instance is available, so instance setup can begin at the correct time. The 2D and 3D map instance hooks use it to connect container and map-element lifecycles. References: src/…/use-callback-ref.ts• L3-8, src/…/use-map-instance.ts• L53-65, src/…/use-map-3d-instance.ts• L23-36

Forced synchronization

useForceUpdate provides a render trigger for state held in a mutable ref. Camera tracking uses this pattern because map events update the latest camera values outside React, then request a render so controlled camera coordination can see those values. The same contract applies to 2D and 3D camera state. References: src/…/use-force-update.ts• L1-6, src/…/use-tracked-camera-state-ref.ts• L37-77, src/…/use-tracked-camera-state-ref-3d.ts• L55-92

Option update relationship

Comparison utilities decide whether an option effect should run, but they do not define which options are safe to update or how removed values are handled. The map and Map3D option hooks filter props and apply changes to live instances; the detailed rules belong to map-object option reconciliation. References: src/…/use-map-options.ts• L42-73, src/…/use-map-3d-options.ts• L24-59

Change checklist

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

  • Keep callback freshness separate from subscription identity. Changing a handler must not cause missed events or leave an old listener attached.
  • Preserve the distinction between an omitted prop and a later undefined value in usePropBinding; this protects API defaults and clear operations.
  • Keep comparison rules aligned with the value shape passed to option effects. A shallow rule can hide nested option changes, while an overly broad rule can cause needless map updates.
  • When adding an event prop, update its event-name mapping and its event-value normalization together for both 2D and 3D event layers where applicable.
  • When an external event updates mutable camera state, keep the render trigger and listener cleanup paired with the same map instance.

Important failure modes

TriggerConsequenceGuard
A listener uses a changing callback as its subscription identityReact repeatedly removes and adds the listener, or an old callback remains activeRoute the callback through useEffectEvent and make the effect depend on the target and event identity
An omitted prop is written as undefined to a new Maps elementThe Maps API default is cleared or emits a warningLet usePropBinding write an initial undefined only after that object has already received a value
A nested option object is compared by identityEquivalent renders trigger repeated option updatesUse the comparison hook that matches the option value shape
A map event listener is not removed when its target changesEvents from an old map update the current React componentReturn cleanup from every subscription effect and remove the exact listener
A mutable camera ref changes without a render triggerControlled camera logic reads stale React-derived coordination statePair external camera updates with useForceUpdate

Key files

FileWhy to read it
src/…/use-effect-event.tsDefines the callback freshness contract shared by event bridges.
src/…/use-memoized.tsShows how equality controls the value seen by dependent effects.
src/…/use-prop-binding.tsDefines the omitted-value and clearing rules for writable map elements.
src/…/use-dom-event-listener.tsShows the DOM subscription lifecycle.
src/…/use-maps-event-listener.tsShows the Maps API subscription lifecycle.
src/…/use-map-events.tsShows how 2D map events become React-facing event values.
src/…/use-map-3d-events.tsShows how 3D custom-element events become React-facing event values.
src/…/use-tracked-camera-state-ref.tsShows mutable camera tracking and render synchronization for 2D maps.
src/…/use-tracked-camera-state-ref-3d.tsShows the matching camera tracking contract for 3D maps.
src/…/use-map-options.tsShows how deep comparison gates live 2D map option updates.
src/…/use-map-3d-options.tsShows how deep comparison gates live 3D map option updates.