Nothing Special   »   [go: up one dir, main page]

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

[Bug] Unable to use Mapbox map.setPaintProperty #2443

Open
newguy123-creator opened this issue Nov 8, 2024 · 3 comments
Open

[Bug] Unable to use Mapbox map.setPaintProperty #2443

newguy123-creator opened this issue Nov 8, 2024 · 3 comments
Assignees
Labels

Comments

@newguy123-creator
Copy link
newguy123-creator commented Nov 8, 2024

Description

So how on earth do I set this property?
map.setPaintProperty('basemap', 'raster-opacity', 0.5)
No matter where I add it, I get console error: this is not a function

Expected Behavior

It should make the map 50% see-through without affecting its container or any of my R3F 3D components.

Steps to Reproduce

    const see = () => {
        mapRef.current.setPaintProperty("basemap", "raster-opacity", 0.5);
    };

return(
<>
                <Map
                    {...viewState}
                    antialias
                    ref={mapRef}
                    mapboxAccessToken="TOKEN"
                    style={{ width: "100%", height: "100%" }}
                    mapStyle="mapbox://styles/mapbox/standard"
                    onMove={(evt) => setViewState(evt.viewState)}
                    onLoad={() => see()}
                >
         
                    <NavigationControl showCompass={true} />
                   
                    <Canvas
                        shadows
                        latitude={lat}
                        longitude={long}
                   >
                        
                        <MainLight />
                        <Site position={[-3.855, 0, 180.526]} />
                        
                    </Canvas>
                   
                </Map>
</>

Environment

  • Framework version: react-map-gl@7.1.7
  • Map library: mapbox-gl@3.7.0"
  • Browser: Chrome 130.0
  • OS: Windows 10 Pro

Logs

Uncaught TypeError: mapRef.current.setPaintProperty is not a function

@chrisgervang
Copy link
Contributor
chrisgervang commented Dec 14, 2024

Is mapRef.current defined? What type is it?

@chrisgervang
Copy link
Contributor

The MapRef is just a react ref containing the mapbox-gl Map object, so as long as it's defined when you reference it then it will behave according to their api docs.

@chrisgervang
Copy link
Contributor

I think I found it. See getMap()

The MapRef object exposes Map methods (Mapbox | Maplibre) that are safe to call without breaking the React bindings. For example, setStyle() is hidden from the ref object, because the style is supposed to be changed by updating the mapStyle prop. Calling the method directly may cause the the React prop to mismatch with the underlying state, and lead to unexpected behaviors.
You can still access the hidden members via getMap()

@chrisgervang chrisgervang self-assigned this Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants