src/docs/configuration.md
There're some steps to take to create your custom map components. Follow on:
### Step 1
**Everything inside a <GoogleMap> component will be mounted automatically on the map**
And it will be automatically unmounted from the map if you don't render it.
```js static
import { GoogleMap, Marker } from "react-google-maps"
const MyMapComponent = (props) =>
<GoogleMap
defaultZoom={8}
defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
{props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
</GoogleMap>
<MyMapComponent isMarkerShown />// Map with a Marker
<MyMapComponent isMarkerShown={false} />// Just only Map
```
### Step 2
In order to initialize the `MyMapComponent` with DOM instances, you'll need to wrap it with [`withGoogleMap`][withGoogleMap] HOC.
```js static
import { withGoogleMap, GoogleMap, Marker } from "react-google-maps"
const MyMapComponent = withGoogleMap((props) =>
<GoogleMap
defaultZoom={8}
defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
{props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
</GoogleMap>
)
<MyMapComponent isMarkerShown />// Map with a Marker
<MyMapComponent isMarkerShown={false} />// Just only Map
```
### Step 3
In order to correctly load [Google Maps JavaScript API v3][gmjsav3], you'll need to wrap it with [`withScriptjs`][withScriptjs] HOC.
```js static
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from "react-google-maps"
const MyMapComponent = withScriptjs(withGoogleMap((props) =>
<GoogleMap
defaultZoom={8}
defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
{props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
</GoogleMap>
))
<MyMapComponent isMarkerShown />// Map with a Marker
<MyMapComponent isMarkerShown={false} />// Just only Map
```
_If you don't use `withScriptjs`, you have to put a `<script/>` tag for [Google Maps JavaScript API v3][gmjsav3] in your HTML's `<head/>` element_
### Step 4
Notice there're some required props for [`withGoogleMap`][withGoogleMap] and [`withScriptjs`][withScriptjs] HOC.
```js static
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from "react-google-maps"
const MyMapComponent = withScriptjs(withGoogleMap((props) =>
<GoogleMap
defaultZoom={8}
defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
{props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
</GoogleMap>
))
<MyMapComponent
isMarkerShown
googleMapURL="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places"
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `400px` }} />}
mapElement={<div style={{ height: `100%` }} />}
/>
```
For _simplicity_, in this documentation, I will use [`recompose`][recompose] to simplify the component. It'll look something like this with `recompose`:
```js static
import { compose, withProps } from "recompose"
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from "react-google-maps"
const MyMapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withScriptjs,
withGoogleMap
)((props) =>
<GoogleMap
defaultZoom={8}
defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
{props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} />}
</GoogleMap>
))
<MyMapComponent isMarkerShown />
```
### Step 5
Implement your own state transition logic with `MyMapComponent`!
```js static
import React from "react"
import { compose, withProps } from "recompose"
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from "react-google-maps"
const MyMapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withScriptjs,
withGoogleMap
)((props) =>
<GoogleMap
defaultZoom={8}
defaultCenter={{ lat: -34.397, lng: 150.644 }}
>
{props.isMarkerShown && <Marker position={{ lat: -34.397, lng: 150.644 }} onClick={props.onMarkerClick} />}
</GoogleMap>
))
class MyFancyComponent extends React.PureComponent {
state = {
isMarkerShown: false,
}
componentDidMount() {
this.delayedShowMarker()
}
delayedShowMarker = () => {
setTimeout(() => {
this.setState({ isMarkerShown: true })
}, 3000)
}
handleMarkerClick = () => {
this.setState({ isMarkerShown: false })
this.delayedShowMarker()
}
render() {
return (
<MyMapComponent
isMarkerShown={this.state.isMarkerShown}
onMarkerClick={this.handleMarkerClick}
/>
)
}
}
```
[withGoogleMap]: https://tomchentw.github.io/react-google-maps/#withgooglemap
[gmjsav3]: https://developers.google.com/maps/documentation/javascript/
[withScriptjs]: https://tomchentw.github.io/react-google-maps/#withscriptjs
[recompose]: https://github.com/acdlite/recompose/blob/master/docs/API.md