Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MapViewCallbacks keep old references for variables #235

Closed
nexttrack07 opened this issue Aug 23, 2024 · 3 comments · Fixed by #239
Closed

MapViewCallbacks keep old references for variables #235

nexttrack07 opened this issue Aug 23, 2024 · 3 comments · Fixed by #239
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@nexttrack07
Copy link
Collaborator

SDK Version: 0.4.3

The MapViewCallbacks are not getting recreated when a state value changes. If I define local state variable and I use that variable inside a callback then the callback always has the initial value of the variable even if the variable gets updated. This happens even when the mapViewCallback is defined using useCallback and the the variable is listed as a dependency.

  const [appointmentMarkers, setAppointmentMarkers] = useState<Array<Marker>>(
    []
  );

const addMarkersToMap = useCallback(() => {
    mapViewController
      ?.isMyLocationEnabled()
      .then(() => {
        console.log('My Location Enabled');
      })
      .catch((e: any) => {
        console.log('My Location Disabled', e);
      });
    const addMarkerCalls = LatLngs.map(latLng => {
      return mapViewController?.addMarker({
        position: latLng,
        visible: true,
      });
    });

    Promise.all(addMarkerCalls).then(markers => {
      console.log('All markers added');
      navigationController.setDestinations(
        LatLngs.map(data => ({ position: data }))
      );

      const nonEmptyMarkers = markers.filter(Boolean) as Marker[];

      setAppointmentMarkers(nonEmptyMarkers);
    });
  }, [mapViewController, navigationController]);

  const onNavigationReady = useCallback(() => {
    console.log('onNavigationReady');

    addMarkersToMap();
  }, [addMarkersToMap]);

  console.log('Markers array (outside callback): ', appointmentMarkers);

const mapViewCallbacks: MapViewCallbacks = {
    onMapReady,
    onMarkerClick: useCallback(
      (marker: Marker) => {
        console.log(
          'onMarkerClick:',
          marker.id,
          appointmentMarkers.map(m => m.id)
        );
        const selectedMarker = appointmentMarkers.find(m => m.id === marker.id);

        console.log('Selected Marker:', selectedMarker);
      },
      [appointmentMarkers]
    ),
    onPolygonClick: (polygon: Polygon) => {
      console.log('onPolygonClick:', polygon);
      mapViewController?.removePolygon(polygon.id);
    },
    onCircleClick: (circle: Circle) => {
      console.log('onCircleClick:', circle);
      mapViewController?.removeCircle(circle.id);
    },
    onPolylineClick: (polyline: Polyline) => {
      console.log('onPolylineClick:', polyline);
      mapViewController?.removePolyline(polyline.id);
    },
    onMarkerInfoWindowTapped: (marker: Marker) => {
      console.log('onMarkerInfoWindowTapped:', marker);
    },
    onMapClick: (latLng: LatLng) => {
      console.log('onMapClick:', latLng);
    },
  };

You can see that there is a console.log with appointmentMarkers inside onMarkerClick and there is one outside. The outside one has the correct value of the appointmentMarkers and is updated. The one inside the onMarkerClick callback always has value [] and never gets updated. This same code existed in version 0.3.4 and worked perfectly.

Screenshot 2024-08-23 at 12 37 31 PM

I've tried putting onMarkerClick in a useCallback, defined it outside then referenced, I've removed the useMemo from mapViewCallbacks and it doesn't make any difference. The value of the state var inside any of mapViewCallbacks always stays the same and never updates.

@nexttrack07 nexttrack07 added triage me I really want to be triaged. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Aug 23, 2024
@jokerttu jokerttu added priority: p2 Moderately-important priority. Fix may not be included in next release. and removed triage me I really want to be triaged. labels Aug 23, 2024
@jokerttu
Copy link
Contributor

Hi @nexttrack07
Please check PR #239, and let me know if it resolves this issue.

@nexttrack07
Copy link
Collaborator Author

Thanks! Will check but doesn't look like the changes are release in a new version yet. Will it be released in 0.4.4?

@jokerttu
Copy link
Contributor

jokerttu commented Sep 4, 2024

@nexttrack07
Version 0.5.0 will be released soon
Note that callbacks are still given as mapViewCallbacks prop. To detect changes, the mapViewCallbacks object need to be updated (as it is demonstrated in the SampleApp).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants