Fix Marker position update comparison #1139
Open
+46
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When rendering list of markers from state array using
.map
and later updating that state, all markers positions will be updated even though no existing marker has changed its position. In some cases this is causing flickering of the markers' icons. (This is noticeable especially when usingMarkerClusterGroup
)Example: https://stackblitz.com/edit/vitejs-vite-lonc2p?file=src%2FMap.tsx (Notice markers' flicker after around 3 secs after the page loads. The most bottom marker will have changed opacity from
1
to0.5
after 3 secs. I implemented changes from this PR in that example. With those changes there is no more flickering)Fix
Current check to decide whether or not to update the position is
props.position !== prevProps.position
, which compares by reference. To update the marker's position only when position value changes, the validation should compare objects by values instead. I added 3 different checks for 3 potentialposition
types:type LatLngExpression = LatLng | LatLngLiteral | LatLngTuple;