(id: string | null = null)
| 10 | * Returns null if neither can be found. |
| 11 | */ |
| 12 | export const useMap = (id: string | null = null): google.maps.Map | null => { |
| 13 | const ctx = useContext(APIProviderContext); |
| 14 | const {map} = useContext(GoogleMapsContext) || {}; |
| 15 | |
| 16 | if (ctx === null) { |
| 17 | logErrorOnce( |
| 18 | 'useMap(): failed to retrieve APIProviderContext. ' + |
| 19 | 'Make sure that the <APIProvider> component exists and that the ' + |
| 20 | 'component you are calling `useMap()` from is a sibling of the ' + |
| 21 | '<APIProvider>.' |
| 22 | ); |
| 23 | |
| 24 | return null; |
| 25 | } |
| 26 | |
| 27 | const {mapInstances} = ctx; |
| 28 | |
| 29 | // if an id is specified, the corresponding map or null is returned |
| 30 | if (id !== null) return mapInstances[id] || null; |
| 31 | |
| 32 | // otherwise, return the closest ancestor |
| 33 | if (map) return map; |
| 34 | |
| 35 | // finally, return the default map instance |
| 36 | return mapInstances['default'] || null; |
| 37 | }; |
no test coverage detected