visgl / react-map-gl

React friendly API wrapper around MapboxGL JS
http://visgl.github.io/react-map-gl/
Other
7.88k stars 1.35k forks source link

[Bug] #2149

Closed knutmarius closed 1 year ago

knutmarius commented 1 year ago

Description

Using React 18 and strict mode, the onRemove handler passed to the useControl hook seems to be called immediately after the onCreate handler, and this is expected due to how strict mode works in React 18. The problem is that the mount function is not called again afterwards, so it stays unmounted. If I disable strict mode, it works as intended.

Expected Behavior

Expect the onCreate function to be called again when the component remounts.

Steps to Reproduce

export default function DrawControl(props: DrawControlProps) {
  const { onInit, ...rest } = props;
  useControl(
    //@ts-ignore
    ({ map }) => {
      const control = new MapboxDraw(rest);
      onInit(control);
      map.on('draw.create', props.onCreate);
      map.on('draw.update', props.onUpdate);
      map.on('draw.delete', props.onDelete);
      return control;
    },
    ({ map }) => {
      map.off('draw.create', props.onCreate);
      map.off('draw.update', props.onUpdate);
      map.off('draw.delete', props.onDelete);
    },
    {
      position: props.position,
    }
  );

  return null;
}

Environment

Logs

No response

Pessimistress commented 1 year ago

Use the useControl(onCreate, onAdd, onRemove, options) signature.

https://visgl.github.io/react-map-gl/docs/api-reference/use-control#signature