Skip to content

Use in React 18: Destroying a component with DistortableImage crashes app #1391

Description

@PeteMac3D

Scope:
React v.18.2.0, vite v.4.4.5, React-Leaflet: v4.2.1, Leaflet.DistortableImage: v0.21.9

Description:
Not sure if this is a bug, my usage, or simply DistortableImage isn't set up for use in React?
Destroying a component results in a crash of the app, specifically if I included the action L.RotateAction or L.LockAction (pass in as an array or used as default) When I have these eliminated everything works as expected.

Think the problem lays with the unbinding of the event handlers and the loss of context relating to 'this' , initialized by React during diffing stage ?

See console error(s)
image

offending line of code in editHandle.js

 _unbindListeners: function() {
    this.off({
      contextmenu: L.DomEvent.stop,
      dragstart: this._onHandleDragStart,
      drag: this._onHandleDrag,
      dragend: this._onHandleDragEnd,
    }, this);

    this._handled._map.off('zoomend', this.updateHandle, this);
    this._handled.off('update', this.updateHandle, this);
  }

useEffect in compoment

useEffect(() => {
    map.whenReady(function () {
      img = L.distortableImageOverlay('/100mlayout.png', {
        actions: [
          L.RotateAction,
          L.OpacityAction,
          L.BorderAction,
          L.ExportAction,
          // L.LockAction,
          L.RestoreAction,
          L.StackAction,
          L.DeleteAction,
        ]


        corners: calulateBounds(map.getCenter()),
      }).addTo(map);
    });

    // return () => map.remove();
  }, []);

Parent component

 return (
    <MapContainer
      className="project__map"
      center={center}
      zoom={zoom}
      // ref={map}
      whenReady={() => {
        setIsReady(true);
      }}
    >
      <ReactLeafletGoogleLayer
        apiKey="******************************************"
        type={'satellite'}
      />
      {isReady && <MapOverlay setIsReady={setIsReady} />}
      <MapSearch
        apiKey="******************************************"
        setIsReady={setIsReady}
      />
      
    </MapContainer>
  );

As a side note I tried to remove the action in useEffect (using the useeffect clean up function) but cannot locate the method 'removeTool' I dont see it on any of the instances prototypes ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions