fix(react-maplibre): guard against undefined map.style in _updateStyleComponents#2580
Open
kevinjmo wants to merge 1 commit intovisgl:masterfrom
Open
fix(react-maplibre): guard against undefined map.style in _updateStyleComponents#2580kevinjmo wants to merge 1 commit intovisgl:masterfrom
kevinjmo wants to merge 1 commit intovisgl:masterfrom
Conversation
…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).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When using React 19's
<Activity>component withmode="hidden", the map can be in a partially initialized state when event handlers fire. This causes aTypeErrorwhen accessingmap.style._loadedbecausemap.styleis undefined.Error:
The fix: Add optional chaining (
map.style?._loaded) to prevent the crash while maintaining the same logical behavior - ifmap.styleis 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 - changedmap.style._loadedtomap.style?._loadedContext
React 19's Activity component with
mode="hidden":mode="visible", effects run againDuring this transition, internal map events (
style.loadorsourcedata) can fire and call_updateStyleComponentsbefore the map is fully reinitialized, causing the null reference error.