|
| 1 | +# Migrating from `@googlemaps/react-wrapper` |
| 2 | + |
| 3 | +The [`@googlemaps/react-wrapper`][npm-react-wrapper] library provides a |
| 4 | +minimal wrapper for loading the Maps JavaScript API in a React Application. |
| 5 | + |
| 6 | +If you decide to migrate from using `@googlemaps/react-wrapper` to this |
| 7 | +library, this can be done seamlessly by using a poyfill for the `<Wrapper>` |
| 8 | +component. |
| 9 | + |
| 10 | +Roughly speaking, our [`<APIProvider>`][rgm-api-provider] component has the |
| 11 | +same function as the `<Wrapper>` component provided by |
| 12 | +`@googlemaps/react-wrapper`, with one major difference: While the `Wrapper` |
| 13 | +component will only render its children once the Google Maps JavaScript API |
| 14 | +has been loaded, the `APIProvider` will always render the children and use |
| 15 | +custom hooks like [`useApiLoadingStatus()`][rgm-use-api-loading-status] to |
| 16 | +handle API loading in its components. |
| 17 | + |
| 18 | +The following code shows how the `Wrapper` component can be implemented with |
| 19 | +this library in a fully compatible way, allowing you to use it as a |
| 20 | +drop-in-replacement for the `@googlemaps/react-wrapper` library. |
| 21 | + |
| 22 | +[A complete example can be found here][rgm-examples-react-wrapper-migration]. |
| 23 | + |
| 24 | +```tsx title="wrapper.tsx" |
| 25 | +import React, { |
| 26 | + FunctionComponent, |
| 27 | + PropsWithChildren, |
| 28 | + ReactNode, |
| 29 | + useEffect |
| 30 | +} from 'react'; |
| 31 | + |
| 32 | +import { |
| 33 | + APILoadingStatus, |
| 34 | + APIProvider, |
| 35 | + APIProviderProps, |
| 36 | + useApiLoadingStatus |
| 37 | +} from '@vis.gl/react-google-maps'; |
| 38 | + |
| 39 | +const STATUS_MAP = { |
| 40 | + [APILoadingStatus.LOADING]: 'LOADING', |
| 41 | + [APILoadingStatus.LOADED]: 'SUCCESS', |
| 42 | + [APILoadingStatus.FAILED]: 'FAILURE' |
| 43 | +} as const; |
| 44 | + |
| 45 | +type WrapperProps = PropsWithChildren< |
| 46 | + { |
| 47 | + apiKey: string; |
| 48 | + callback?: (status: string) => void; |
| 49 | + render?: (status: string) => ReactNode; |
| 50 | + } & APIProviderProps |
| 51 | +>; |
| 52 | + |
| 53 | +export const Wrapper: FunctionComponent<WrapperProps> = ({ |
| 54 | + apiKey, |
| 55 | + children, |
| 56 | + render, |
| 57 | + callback, |
| 58 | + ...apiProps |
| 59 | +}) => { |
| 60 | + return ( |
| 61 | + <APIProvider apiKey={apiKey} {...apiProps}> |
| 62 | + <InnerWrapper render={render}>{children}</InnerWrapper> |
| 63 | + </APIProvider> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +const InnerWrapper = ({ |
| 68 | + callback, |
| 69 | + render, |
| 70 | + children |
| 71 | +}: PropsWithChildren<Omit<WrapperProps, 'apiKey'>>) => { |
| 72 | + const status = useApiLoadingStatus(); |
| 73 | + const mappedStatus = STATUS_MAP[status] ?? 'LOADING'; |
| 74 | + |
| 75 | + useEffect(() => { |
| 76 | + if (callback) callback(mappedStatus); |
| 77 | + }, [callback, mappedStatus]); |
| 78 | + |
| 79 | + if (status === APILoadingStatus.LOADED) return children; |
| 80 | + if (render) return render(mappedStatus); |
| 81 | + |
| 82 | + return <></>; |
| 83 | +}; |
| 84 | +``` |
| 85 | + |
| 86 | +[npm-react-wrapper]: https://www.npmjs.com/package/@googlemaps/react-wrapper |
| 87 | +[rgm-api-provider]: ../api-reference/components/api-provider.md |
| 88 | +[rgm-use-api-loading-status]: ../api-reference/hooks/use-api-loading-status.md |
| 89 | +[rgm-examples-react-wrapper-migration]: https://github.com/visgl/react-google-maps/tree/main/examples/react-wrapper-migration |
0 commit comments