Official integration patterns for Mapbox GL JS across popular web frameworks (React, Vue, Svelte, Angular). Covers setup, lifecycle management, token handling, search integration, and common pitfalls. Based on Mapbox's create-web-app scaffolding tool.
This skill provides official patterns for integrating Mapbox GL JS into web applications using React, Vue, Svelte, Angular, and vanilla JavaScript. These patterns are based on Mapbox's create-web-app scaffolding tool and represent production-ready best practices.
Version Requirements
Mapbox GL JS
Recommended: v3.x (latest)
- Minimum: v3.0.0
Why v3.x: Modern API, improved performance, active development
v2.x: Legacy; no longer actively developed (see migration notes below)
Installing via npm (recommended for production):
CODEBLOCK0
CDN (for prototyping only):
CODEBLOCK1
Framework Requirements
React: GL JS works with React 16.8+ (requires hooks). create-web-app scaffolds with React 19.x. Vue: GL JS works with Vue 2.x+ (Vue 3 Composition API recommended). Svelte: GL JS works with any Svelte version. create-web-app scaffolds with Svelte 5.x. Angular: GL JS works with Angular 2+. create-web-app scaffolds with Angular 19.x. Next.js: Minimum 13.x (App Router), Pages Router 12.x+.
Mapbox Search JS
CODEBLOCK2
Version Migration Notes (v2.x to v3.x)
- WebGL 2 now required
INLINECODE4 option removed
Improved TypeScript types, better tree-shaking support
No breaking changes to core initialization patterns
Token patterns (work in v2.x and v3.x):
CODEBLOCK3
Core Principles
Every Mapbox GL JS integration must:
1. Initialize the map in the correct lifecycle hook
Store map instance in component state (not recreate on every render)
Always call map.remove() on cleanup to prevent memory leaks
Note: These examples use Vite (the bundler used in create-web-app). If using Create React App, replace import.meta.env.VITE_MAPBOX_ACCESS_TOKEN with process.env.REACT_APP_MAPBOX_TOKEN. See Token Management Patterns for other bundlers.
CODEBLOCK4
Key points:
- Use useRef for both map instance and container
Initialize in useEffect with empty deps INLINECODE12
Always return cleanup function that calls INLINECODE13
Never initialize map in render (causes infinite loops)
React + Search JS
CODEBLOCK5
Search JS Integration Summary
Install:
CODEBLOCK6
Both packages include @mapbox/search-js-core as a dependency. Only install -core directly if building a custom search UI.
Key configuration options:
- accessToken: Your Mapbox public token
INLINECODE17 : Map instance (must be initialized first)
INLINECODE18 : The mapboxgl library reference
INLINECODE19 : [lng, lat] to bias results geographically
INLINECODE21 : Boolean to show/hide result marker
INLINECODE22 : Search box placeholder text
Positioning Search Box
Absolute positioning (overlay):
CODEBLOCK7
Common positions:
- Top-right: INLINECODE23
Top-left: INLINECODE24
Bottom-left: INLINECODE25
Common Mistakes (Critical)
Mistake 1: Forgetting to call map.remove()
CODEBLOCK8
Why: Every Map instance creates WebGL contexts, event listeners, and DOM nodes. Without cleanup, these accumulate and cause memory leaks.
Mistake 2: Initializing map in render
CODEBLOCK9
Why: React components re-render frequently. Creating a new map on every render causes infinite loops and crashes.
Mistake 3: Not storing map instance properly
CODEBLOCK10
Why: You need to access the map instance for operations like adding layers, markers, or calling remove().
Mistake 4: Storing map instance in Vue's data() (Vue-specific)
CODEBLOCK11
Why: In Vue (especially Vue 3), data() properties are wrapped in a Proxy for reactivity. Mapbox GL JS internally checks object identity and uses properties that don't survive proxy wrapping. Storing the map in data() causes subtle, hard-to-debug failures. Instead, assign the map instance directly as this.map in mounted() — properties assigned outside data() are not made reactive.
Reference Files
Load these for framework-specific patterns and additional details:
Initial release of official Mapbox GL JS integration patterns for major web frameworks.
- Provides production-ready examples for React, Vue, Svelte, Angular, and vanilla JS.
- Covers core topics: setup, lifecycle management, token handling, search integration, and avoiding common pitfalls.
- Includes up-to-date patterns for Mapbox GL JS v3.x and migration notes from v2.x.
- Features best practices for cleanup (`map.remove()`), map instance storage, and secure token usage.
- Offers sample integration with Mapbox Search JS for React and other frameworks.