Overview
useLoadScript is a React hook that loads the Google Maps JavaScript API by injecting a script tag into the document. This provides an alternative to useJsApiLoader with additional control over the script URL.
Signature
Parameters
Configuration options for loading the Google Maps API.
Your Google Maps API key. Either this or
googleMapsClientId is required.Your Google Maps Client ID (for Premium Plan customers). Either this or
googleMapsApiKey is required.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:
'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.
Channel parameter for Google Maps Premium Plan customers to track usage across different applications.
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. Only ‘origin’ is supported.
The base URL for the Google Maps API. Useful for testing or using alternative endpoints.
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.The complete URL that was used to load the Google Maps API script.
Usage Example
With Client ID (Premium Plan)
Static Libraries Configuration
Notes
- Authentication: You must provide either
googleMapsApiKeyorgoogleMapsClientId, but not both. - 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. - Script Caching: The hook caches the previously loaded URL to avoid reloading the script if the same URL is requested again.
- URL Access: Unlike
useJsApiLoader, this hook returns the complete script URL, which can be useful for debugging or logging. - Browser Only: The script loading only occurs in browser environments. Server-side rendering will skip the loading process.