fix markers rerender when click to open InfoWindow by deadkff01 · Pull Request #310 · fullstackreact/google-maps-react
Solving issue #269
In Marker.js componentDidUpdate()
this.props.position and prevProps.position will never be equal after component is fully rendered in first time, because these properties are objects, for this reason I use the lat and lng to compare after the first rendering.
@deadkff01 , I came across that issue recently. I did the same comparison but in shouldComponentUpdate method. Can I ask you why you've opted to the componentDidUpdate?
Thanks
@m-bartenev, I did the comparison in componentDidUpdate only to keep the same code structure. :)
To anyone who comes across this bug, here is a patch you can use until things are merged, should stop the constant re-renders:
class CustomMarker extends Marker {
componentDidUpdate(prevProps) {
if(
this.props.map !== prevProps.map ||
this.props.icon.url !== prevProps.icon.url ||
(
this.props.position.lat !== prevProps.position.lat ||
this.props.position.lng !== prevProps.position.lng
)
) {
if(this.marker) {
this.marker.setMap(null);
}
this.renderMarker();
}
}
}
| componentDidUpdate(prevProps) { | ||
| if ((this.props.map !== prevProps.map) || | ||
| (this.props.position !== prevProps.position) || | ||
| ((this.props.position && prevProps.position) |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
most unreadable, please rather use a function such as 'arePositionsEqual' to simplify readability. thank you
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it's been a long time, I'll see if this change still makes sense, if so, I'll make the change later.
Thanks for the feedback.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem still persists in the current version of the lib.
I solved your issue in the last commits.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters