- What’s shown to the user when a component calls render() is conveniently managed internally allowing an implementor to work on higher level tasks like how an interface behaves.What can be confusing is connecting React with other libraries that manipulate the DOM and manage state (like Mapbox GL JS).
- The wrapper permits customization like size or title, and the technical details that should always be the same (event handling of key bindings or accessibility) is tucked away in the lower level modal component.So it’s natural to look for a Mapbox / component within the React ecosystem (see react-map-gl or react-mapbox-gl as example).
- The map is initialized within componentDidMount and its container value is set as the assignment of this.mapContainer which is React’s way of providing direct access to the DOM node.Let’s explore some more in-depth examples:Basic exampleSee full example on GitHubIn this example, React passes position data as state to the map.
- This could also be prop data passed from a higher level component but in the interest of containing the entire app as one component I’ve used state.Reactive tooltip exampleSee full example on GitHubThe details to note are:Data found in vector tiles is collected under the mouse cursor using queryRenderedFeaturesA mapboxgl.Marker instance is used to display the collected data on the map (Mapbox GL JS controls its position on the map) but the contents of the marker is React powered using ReactDOM.render.Fetching map data like this isn’t exclusive to tiles directly from Mapbox.
- To learn more how we use Redux in Mapbox Studio, check out, Redux for state management in large web apps by David Clark.I hope this helps as a primer on using Mapbox GL JS alongside React and provides some context for how we achieve a few different concepts here at Mapbox.
When we build web applications at Mapbox we often turn to React and Mapbox GL JS. The goals of each library work powerfully in combination and I’d like to share some techniques into how we approach…
Continue reading “Mapbox GL JS + React – Points of interest”