Markers update when Markers array is updated - possible? #362
-
Is there a way to do as the title says? I want the markers on the map to update when the markers array is updated. Or the easiest / cleanest way to accomplish this? Code: declaring the markers array:
the return:
when I do this the markers on the map should be updated:
So far I have only had success by using the reference to the marker as outlined here: https://visgl.github.io/react-google-maps/docs/api-reference/components/advanced-marker#useadvancedmarkerref and then doing this:
but it involves a lot of extra code and seems not the greatest way. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I suspect the problem here is that you aren't actually changing the identity of the Can you give this a try: Instead of |
Beta Was this translation helpful? Give feedback.
-
Yes that was it now it works perfectly 👏😀 |
Beta Was this translation helpful? Give feedback.
I suspect the problem here is that you aren't actually changing the identity of the
markers
array. React will basically only update ifoldThing !== newThing
, which isn't the case if you manipulate the array in place.Can you give this a try: Instead of
markers.pop(); setMarkers(markers);
, usemarkers.pop(); setMarkers([...markers]);
, creating a copy of the array before setting it as new state.