-
Notifications
You must be signed in to change notification settings - Fork 935
Non-Recompose examples #636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You can totally use vanilla React without Just notice three things when using it:
From that, it should be: const MyMapComponent = withScriptjs(withGoogleMap(props => {
return <GoogleMap><Marker /></GoogleMap>
}))
<MyMapComponent
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%` }} />}
/> |
Could you show an an example using |
@sangel10 you just use the |
That works for simple examples but what about an example using nested components? |
@sangel10 I updated the example for you. Is it clear enough now? |
You can also create a wrapper of the form: GoogleMapsWrapper.js import React from 'react';
import { GoogleMap,withGoogleMap,withScriptjs } from 'react-google-maps';
export default const GoogleMapsWrapper = withScriptjs(withGoogleMap(props => {
return <GoogleMap {...props} ref={props.onMapMounted}>{props.children}</GoogleMap>
})); Then use it as main.js import React from 'react';
import GoogleMapsWrapper from './GoogleMapsWrapper.js';
import { Marker } from 'react-google-maps';
import MarkerClusterer from "react-google-maps/lib/components/addons/MarkerClusterer";
class DemoApp extends React.Component {
componentWillMount() {
this.setState({ markers: [] })
}
componentDidMount() {
const url = [
// Length issue
`https://gist.githubusercontent.com`,
`/farrrr/dfda7dd7fccfec5474d3`,
`/raw/758852bbc1979f6c4522ab4e92d1c92cba8fb0dc/data.json`
].join("")
fetch(url)
.then(res => res.json())
.then(data => {
this.setState({ markers: data.photos });
});
}
render () {
return (
<GoogleMapsWrapper
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%` }}
defaultZoom={3}
defaultCenter={{ lat: 25.0391667, lng: 121.525 }}>
<MarkerClusterer
averageCenter
enableRetinaIcons
gridSize={60}>
{this.state.markers.map(marker => (
<Marker
key={marker.photo_id}
position={{ lat: marker.latitude, lng: marker.longitude }}
/>
))}
</MarkerClusterer>
</GoogleMapsWrapper>
);
}
} |
@MonsieurV your
After these changes @MonsieurV's code works for me. Does this look good to everyone? Would be very helpful to see something like this in the docs. |
Ran into an issue with that ES6 React code. When I attempted to add an onBoundsChanged callback the getBounds and getCenter functions threw an error. Eventually found that I was passing in the onMapMounted prop incorrectly, decided to post here for posterity. GoogleMapsWrapper.js:
map.jsx
|
@JarLowrey yeah, because |
For anyone reading this in the future, please NEVER put functions in the state like @JarLowrey did, this is not a good practice. The state is for keeping internal data relative to the rendering of your component and nothing else PS: As shown in the code, the ref can be Use this safe way to set the ref (with the help of @MonsieurV's wrapper) edited multiple times to include a working PoC: import React from 'react';
import {GoogleMap, Marker, withGoogleMap, withScriptjs} from 'react-google-maps';
import MarkerClusterer from "react-google-maps/lib/components/addons/MarkerClusterer";
const GoogleMapsWrapper = withScriptjs(withGoogleMap(props => {
const {onMapMounted, ...otherProps} = props;
return <GoogleMap {...otherProps} ref={c => {
onMapMounted && onMapMounted(c)
}}>{props.children}</GoogleMap>
}));
export default class MapPage extends React.Component {
state = {
markers: [],
};
componentDidMount() {
console.log('Mounted @ ' + Date.now());
const url = "https://gist.githubusercontent.com/farrrr/dfda7dd7fccfec5474d3/raw/758852bbc1979f6c4522ab4e92d1c92cba8fb0dc/data.json";
fetch(url)
.then(res => res.json())
.then(data => {
this.setState({markers: data.photos});
});
}
_mapRef = null;
_handleMapMounted = (c) => {
if (!c || this._mapRef) return;
this._mapRef = c;
console.log('Ref set later @ ' + Date.now());
};
_handleBoundsChanged = () => {
if (!this._mapRef) return;
const center = this._mapRef.getCenter();
const bounds = this._mapRef.getBounds();
// console.log(center, bounds);
};
render() {
return (
<GoogleMapsWrapper
googleMapURL="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places"
loadingElement={<div style={{height: `100%`}}/>}
containerElement={<div style={{height: `100%`}}/>}
mapElement={<div style={{height: `100%`}}/>}
defaultZoom={3}
defaultCenter={{lat: 25.0391667, lng: 121.525}}
onMapMounted={this._handleMapMounted}
onBoundsChanged={this._handleBoundsChanged}>
<MarkerClusterer
averageCenter
enableRetinaIcons
gridSize={60}>
{this.state.markers.map(marker => (
<Marker
key={marker.photo_id}
position={{lat: marker.latitude, lng: marker.longitude}}
/>
))}
</MarkerClusterer>
</GoogleMapsWrapper>
)
}
} PS2: Other bad practices spotted:
|
@VinceBT I copy/pasted from the code in the documentation. If this is poor design, which I can understand, perhaps the react-google-maps docs should be updated? |
Always take some steps back with React since it is a growing framework, there are some ideologies that have grown around it, some are good, some are bad, it's your job to not spread bad practices. Every time I see your code I notice a new one and I update my post to include it. Could you indicate the precises blocks with the bad practices I mentionned that you copied ? If there are, it would be a great idea to submit a PR then. |
Map with a SearchBox, Standalone SearchBox has a function in the state that sets state. Map with a MarkerClusterer calls setState in componentWillMount. Standalone SearchBox and Map with a SearchBox use refs in componentWillMount. |
Well that's a pretty bad doc then. |
@VinceBT I used your PoC code and just copy pasted it onto a new file and imported the MapPage component in my index.js. I see the markers being imported and the console logs for the refs, but the app screen is blank. Is there something I am missing? My React and react-dom versions are 16.4.0 each. |
where can i get "google" ?
My component:
|
@hungdt-ibl |
@VinceBT thanks for the code snippet above, huge help! Was struggling with this for a while. |
For posterity, if anyone wants a more fleshed-out example...
|
Seems this library was written to be used with just React. The documentation doesn't say it depends on recompose. I've got it working without recompose.
Is there a reason this react package is documented with recompose only?
Unwinding what recompose was doing so I could write it in just React(es6/7) was painful and I am sure other don't want to go through that too. Though i'd ask in case there was a something that I was missing.
Thanks.
The text was updated successfully, but these errors were encountered: