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
Stop map from re-rendering every time state changes #220
Comments
You can use a wrapper component and implement the method shouldComponentUpdate:
|
Thanks, that was helpful. |
Glad it's resolved! We're also looking for maintainers. Involve in #266 to help strengthen our community! |
In case if anyone faced same issue and struggled like me, here is a sample component based on comment above.
it avoids |
I am having the exact same issue, except I don't want a read only map and I need to load the updated props... My code is here, shouldComponentUpdate(nextProps, nextState) {
if (this.props.profile != nextProps.profile) {
return true;
}
if (this.props.map != nextProps.map) {
return true;
}
if (this.state == nextState) {
return false;
}
return true;
} |
Okay - I found the problem, the import React from 'react';
import { withGoogleMap, GoogleMap, Marker } from "react-google-maps";
const RenderMap = withGoogleMap(props => (
<GoogleMap
ref={props.onMapLoad}
onClick={props.onMapClick}
defaultZoom={13}
center={props.center}>
{
props.markers.map(marker => (
<Marker
{...marker}
/>
))
}
</GoogleMap >
));
class Map extends React.Component {
constructor(props) {
super(props);
}
handleMapLoad(map) {
this._map = map;
}
handleMapClick(event) {
console.log(event);
}
render() {
return (
<div>
<RenderMap
onMapLoad={this.handleMapLoad}
onMapClick={this.handleMapClick}
{...etc}
/>
</div>
)
}
}
export default Map; |
Previously, the map reloaded everytime a new area or a new neighbouthood was selected. This was unnecessary as only the markers should be re- rendered. Seperating out the map wrapper (RenderMap) from the Map component fixed this issue. tomchentw/react-google-maps#220 (comment)
@NickBrooks I was just about to pull the plug on this component but I knew I must be doing something wrong since it's so popular. I never would have guessed to pull the definition out of the component! Thank you! |
my map is flickering on outside change.I have a scenario where marker will change dynamically on outer change(prop).Each time my data changes my map starts dancing.Please help me out with the solution or any concept which will help me. |
If anyone struggling with the above mention problem, the way I tackle is to check a single state in shouldComponentUpdate() function. every time I need to re-render the map I would update the state and let shouldComponentUpdate() return true to re-render the map.
|
I have used package '@react-google-maps/api' & separate components for map & markers. The data for both components from redux state. I have to call API in every 5 seconds and the data have to dispatch to redux store. An update occurs in redux state causes the map & marker re-render. Then, How I fix that ? |
nice one ! |
Maybe I'm doing something else wrong, but right now every time my application state gets changed the map component goes gray and re-draws the same map with the same marker. On my use case I actually don't need any interaction with the map, I just want to display the marker and I know it will not change in the future, so I was wondering, is there a way to have like a read-only mode for the map? I want just to render the map one time and to for the map to stay like that even if the there are some state changes.
The text was updated successfully, but these errors were encountered: