Skip to content

Use a ref for useMapTemplate#53

Closed
SamuelBrucksch wants to merge 2 commits intomasterfrom
mapref
Closed

Use a ref for useMapTemplate#53
SamuelBrucksch wants to merge 2 commits intomasterfrom
mapref

Conversation

@SamuelBrucksch
Copy link
Contributor

@SamuelBrucksch SamuelBrucksch commented Feb 23, 2026

This is a breaking change. We probably should make a version 0.4 if we want to do it like this.

The issue is, that on disconnect when we have a logic like

const mapTemplate = useMapTemplate()

useEffect(() => {
  mapTemplate.showTripSelector()
  return () => {
    mapTemplate.hideTripSelector()
  }
}, 
[mapTemplate, ...otherProps])

where we actually want to hide the trip selector based on other props, the mapTemplate would actually not be valid anymore in the cleanup function on unmount. However it is still defined, so calling that action we will run the native code, although it is already in the process of tearing down everything. Right now this would cause an exception, that the scene is not valid anymore.

When using a ref like in this PR we would be able, to immediately reset the state of the mapTemplate, even if the UI wouldn't render anymore. So the useEffect would look like this:

const mapTemplateRef = useMapTemplate()

useEffect(() => {
  mapTemplateRef.current?.showTripSelector()
  return () => {
    mapTemplate.current?.hideTripSelector()
  }
}, 
[mapTemplateRef.current, ...otherProps])

And the cleanup function wouldn't do anything, as the ref is already set to null when getting there.

This could potentially replace #52 as we wouldn't have to check for the connection state on native side. We could however still keep the logic about not throwing when the scene is gone. Not sure though if we actually need it...

@SamuelBrucksch SamuelBrucksch marked this pull request as ready for review February 23, 2026 06:19
@github-actions
Copy link
Contributor

Several issues with dependency arrays using ref.current were found, which can lead to unreliable effects since ref updates do not trigger re-renders.

@g4rb4g3
Copy link
Contributor

g4rb4g3 commented Feb 24, 2026

Closing in favor of #54

@g4rb4g3 g4rb4g3 closed this Feb 24, 2026
@g4rb4g3 g4rb4g3 deleted the mapref branch February 24, 2026 11:09
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.

2 participants