LoadScript
TheLoadScript component loads the Google Maps JavaScript API and manages its lifecycle. It ensures the script is loaded before rendering child components and handles cleanup when unmounted.
Import
TypeScript Signature
Props
Required Props
Unique identifier for the script tag. Used to prevent duplicate script loading.
Your Google Maps API key. Get one from the Google Cloud Console.Note: Either
googleMapsApiKey or googleMapsClientId is required, but not both.Authentication & Configuration
Google Maps Client ID for Premium Plan users. Alternative to API key.
The version of the Google Maps JavaScript API to load.Options:
'weekly'- Latest weekly release (recommended)'quarterly'- Quarterly release'beta'- Beta version- Specific version number (e.g.,
'3.55')
Array of Google Maps libraries to load. Available libraries:Important: Keep this array constant to avoid unnecessary reloads. Define it outside your component or use
'drawing'- Drawing tools'geometry'- Geometry utilities'localContext'- Local context'places'- Places API'visualization'- Heatmaps'marker'- Advanced markers'maps'- Core maps library
useMemo.The language to use for map labels and controls. Uses ISO 639-1 codes.
The region to use for biasing geocoding results. Uses ccTLD (country code top-level domain) format.
Array of Map IDs for use with Cloud-based map styling.
Channel parameter for Premium Plan usage tracking.
Sets the authentication referrer policy. Currently only
'origin' is supported.Custom API URL for loading the Google Maps script. Useful for proxying requests.
Loading & Lifecycle
Child components to render after the Google Maps API has loaded successfully.
Custom loading element to display while the script is loading. If not provided, displays
"Loading...".Callback fired when the Google Maps API has loaded successfully.
Callback fired when there’s an error loading the Google Maps API.
Callback fired when the component is unmounted and cleanup begins.
Advanced Options
Nonce attribute for the script tag, required for Content Security Policy (CSP) compliance.
Prevents Google Maps from automatically loading Roboto and Google Sans fonts.
Usage Examples
Basic Usage
With Libraries
Custom Loading Element
With Error Handling
With CSP Compliance
Important Notes
Libraries Array
Thelibraries prop should be kept constant to avoid unnecessary script reloads. Define it outside your component or use useMemo:
Language Changes
Changing thelanguage prop will trigger a script reload and cleanup, which can cause flickering and performance issues. Keep the language constant or use route-based rendering.
Script Cleanup
WhenLoadScript unmounts, it removes all Google Maps scripts, styles, and related resources from the DOM. This ensures a clean state if the component is remounted.
Performance Considerations
- Consider using LoadScriptNext for better performance in production
- Use useLoadScript hook for more control and to avoid wrapper components
- Load only the libraries you need
- Keep
librariesarray constant to prevent reloads
See Also
- LoadScriptNext - Enhanced version with better performance
- useLoadScript - Hook-based alternative
- useJsApiLoader - Modern loader using @googlemaps/js-api-loader
- GoogleMap - Main map component