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] mapRef.current is null inside `onLoad` #2412

Closed jgarplind closed 4 months ago

jgarplind commented 4 months ago

Description

Using a simple implementation inspired by @Pessimistress in https://github.com/visgl/react-map-gl/discussions/1983#discussioncomment-3615365, I find that the mapRef is null at the time onLoad is called.

Doing something like this:

      onLoad={({ target }) => {
        const map = target as MapboxMap;
        console.log("current event", map);
        console.log("current", mapRef.current);
        setTimeout(() => console.log("current later", mapRef.current), 10);
      }}

Expected Behavior

mapRef.current should be non-null by the time onLoad is called.

Steps to Reproduce

See above.

Environment

Logs

No response

Pessimistress commented 4 months ago

Can you create a CodeSandbox that reproduces your issue?

jgarplind commented 4 months ago

Can you create a CodeSandbox that reproduces your issue?

It didn't reproduce, and I could in fact not even reproduce it in our own repository now that I tried again.

I will close this as "can't repro", and re-open only if I can reproduce it consistently in a place where you too can observe it.

Thanks for taking the time to respond!

lsthornt commented 4 months ago

I am also seeing this behavior in our project. If I can find some time, I will look into creating a CodeSandbox. For now, the steps are:

The behavior is quite reproducible for us, and goes away when disabling reuseMaps

lsthornt commented 4 months ago

First time digging to this codebase here, but it looks like the ref's value (contextValue.map) set set after the call to Mapbox.reuse, where the "load" event is fired: https://github.com/visgl/react-map-gl/blob/master/src/components/map.tsx#L86