Overview
useJsApiLoader is a React hook that loads the Google Maps JavaScript API using the official @googlemaps/js-api-loader package. This is the recommended approach for loading the Google Maps API in modern applications.
Signature
Parameters
Configuration options for loading the Google Maps API.
Your Google Maps API key. Required for authentication.
The ID attribute for the script element.
The version of the Google Maps API to load. Can be ‘weekly’, ‘quarterly’, ‘beta’, or a specific version number.
Array of Google Maps libraries to load. Available libraries include:
'maps'- Core maps library'places'- Places library'geometry'- Geometry library'drawing'- Drawing library'visualization'- Visualization library'marker'- Advanced markers
The language to use for UI elements and display of labels on map tiles.
The region code to use for biasing geocoding results and map behavior.
Array of map IDs for use with advanced styling and features.
A nonce value for Content Security Policy (CSP) compliance.
Specifies the referrer policy for API requests.
When true, prevents Google Maps from loading Google Fonts automatically.
Return Value
Indicates whether the Google Maps API has finished loading successfully.
Contains any error that occurred during loading, or
undefined if no error occurred.Usage Example
With Multiple Libraries
Notes
- Performance Warning: Keep the
librariesarray as a static constant outside your component. Passing a new array on each render will cause the API to reload, which impacts performance. - Loader Instance: This hook uses the official
@googlemaps/js-api-loaderpackage internally, which provides better performance and reliability. - Default Values: The hook applies sensible defaults (language: ‘en’, region: ‘US’, libraries: [‘maps’]).
- Error Handling: Always check both
isLoadedandloadErrorto handle loading states properly.