The map that felt slow: a counter against a stale denominator
From Kosher Kav
- React Native
- performance
- maps
Symptom
After a filter tap on the map screen of Kosher Kav, panning went from smooth to sticky. Not broken — sticky. Frame drops were visible on a mid-range Android device and invisible on the iPhone I developed on, which is why it survived as long as it did.
Why the UI couldn't show it
react-native-maps markers have a flag, tracksViewChanges. While it is true the marker is re-rasterized every frame so that custom content can animate; once the content is stable you set it false and the marker becomes a static bitmap. Our custom pins had an image inside them, so the flag was held true until the image had loaded and then flipped to false. There was no state for "stuck at true" — the pins looked identical either way.
Diagnosis
The flip was driven by a counter: every pin reported when its image loaded, and when loaded === total the screen set tracksViewChanges to false on all of them. total was captured when the pin list was first built. A filter tap replaced the list with a shorter one, the new pins reported their loads, and loaded climbed to the new length — which was never equal to the old total. The condition simply never became true again, so every pin on screen stayed in "re-rasterize me" mode until the app restarted.
// Before: `total` is from the render that created the ref.
const total = useRef(pins.length);
const onLoad = () => {
loaded.current += 1;
if (loaded.current === total.current) setTracking(false);
};Fix
Track per marker, not per screen. Each pin owns its own tracksViewChanges and flips it on its own onLoad. There is no shared denominator to go stale, and a pin that is removed takes its state with it.
const Pin = ({ uri }: { uri: string }) => {
const [tracking, setTracking] = useState(true);
return (
<Marker tracksViewChanges={tracking}>
<Image source={{ uri }} onLoad={() => setTracking(false)} />
</Marker>
);
};Test
A render test mounts ten pins, fires onLoad on each, applies a filter that keeps four, fires onLoad on those, and asserts that every mounted Marker has tracksViewChanges false. The old code fails it at the filter step. It is a cheap test for an expensive class of bug: any counter compared against a value that can change out from under it.