Skip to content

fix(react-maplibre): guard against undefined map.style in _updateStyleComponents#2580

Open
kevinjmo wants to merge 1 commit intovisgl:masterfrom
kevinjmo:fix/guard-map-style-null-check
Open

fix(react-maplibre): guard against undefined map.style in _updateStyleComponents#2580
kevinjmo wants to merge 1 commit intovisgl:masterfrom
kevinjmo:fix/guard-map-style-null-check

Conversation

@kevinjmo
Copy link

@kevinjmo kevinjmo commented Feb 3, 2026

Summary

When using React 19's <Activity> component with mode="hidden", the map can be in a partially initialized state when event handlers fire. This causes a TypeError when accessing map.style._loaded because map.style is undefined.

Error:

TypeError: undefined is not an object (evaluating 'map.style._loaded')

The fix: Add optional chaining (map.style?._loaded) to prevent the crash while maintaining the same logical behavior - if map.style is undefined, the condition is falsy and the style components won't be updated (which is the correct behavior when the map isn't fully initialized).

Changes

  • modules/react-maplibre/src/maplibre/maplibre.ts: Line 465 - changed map.style._loaded to map.style?._loaded

Context

React 19's Activity component with mode="hidden":

  • Runs cleanup effects (like unmounting)
  • Preserves React state
  • When switching back to mode="visible", effects run again

During this transition, internal map events (style.load or sourcedata) can fire and call _updateStyleComponents before the map is fully reinitialized, causing the null reference error.

…eComponents

When using React 19's Activity component with mode="hidden", the map can
be in a partially initialized state when event handlers fire. This causes
a TypeError when accessing map.style._loaded because map.style is undefined.

Adding optional chaining prevents the crash while maintaining the same
logical behavior - if map.style is undefined, the condition is falsy and
the style components won't be updated (which is the correct behavior when
the map isn't fully initialized).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant