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] useControl not working correctly in strict mode #2289

Closed Alexnortung closed 1 year ago

Alexnortung commented 1 year ago

Description

the useControl hook creates the control twice when using strict mode.

It seems like an incorrect use of useMemo

Expected Behavior

I would expect the hook to correctly remove the control on unmount.

Steps to Reproduce

https://codesandbox.io/s/cranky-dust-t7l97m?file=/src/index.js

Environment

Logs

No response

Pessimistress commented 1 year ago

useControl calls map.removeControl on unmount, which is the correct behavior for strict mode. Some native controls, such as GeolocateControl in your example, do not properly remove their async operation callbacks on removal. See https://github.com/visgl/react-map-gl/blob/7.1-release/src/components/geolocate-control.ts#L46