Skip to main content
skill CX Codex

mapbox-web-integration-patterns — Claude Code skill: Mapbox GL JS

Official Mapbox GL JS integration patterns for React, Vue, Svelte, Angular and vanilla JavaScript. Useful when a map leaks memory or refuses to initialize in the right hook.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add mapbox/mapbox-agent-skills --skill mapbox-web-integration-patterns -a claude-code
Open on GitHub

What mapbox-web-integration-patterns does

The skill collects practices from Mapbox's create-web-app scaffolding tool and covers how to wire Mapbox GL JS into a web app. Inside: version requirements, token handling, Mapbox Search JS integration and a list of common mistakes. For React the primary pattern is useRef for both the map instance and the container, initialization inside useEffect with an empty dependency array, and a cleanup function that calls map.remove().

GL JS v3.x is recommended, v3.0.0 is the minimum. The v2.x line is no longer actively developed, and v3 requires WebGL 2 while dropping the optimizeForTerrain option. Tokens belong in environment variables: the examples use import.meta.env.VITE_MAPBOX_ACCESS_TOKEN for Vite and process.env.REACT_APP_MAPBOX_TOKEN for Create React App. With several maps on one page, a per-map token beats the global mapboxgl.accessToken.

There is a separate section on leaks. Every map instance creates WebGL contexts, event listeners and DOM nodes. Skip the cleanup return in useEffect and they pile up. Initializing the map in render breaks things too, because it causes infinite loops. Worth checking your own components if a map outlives a single screen.

What it helps with

  • embed a Mapbox map in a React component without leaking memory
  • move a Mapbox token into environment variables instead of hardcoding it
  • wire address search through Mapbox Search JS
  • migrate from GL JS v2.x to v3.x without surprises
  • find out why the map gets recreated on every render

Example prompts for the agent

  • show me the React pattern for Mapbox GL JS with useRef and useEffect
  • how do I clean up the map instance when the component unmounts?
  • hook up SearchBox from @mapbox/search-js-react to my map

How to install mapbox-web-integration-patterns

1Skills CLI (Vercel)

npx skills add mapbox/mapbox-agent-skills --skill mapbox-web-integration-patterns -a claude-code

2Manually: put the files in place

~/.claude/skills/mapbox-web-integration-patterns/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/mapbox-web-integration-patterns/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about mapbox-web-integration-patterns

How do I install mapbox-web-integration-patterns in Claude?

Run npx skills add mapbox/mapbox-agent-skills --skill mapbox-web-integration-patterns -a claude-code. The skill lands in ~/.claude/skills/mapbox-web-integration-patterns/SKILL.md.

What does mapbox-web-integration-patterns do?

It hands the agent official Mapbox GL JS integration patterns: map lifecycle, token handling, Search JS setup and mistakes like a missing map.remove().

Which Mapbox GL JS version does it need?

v3.x is recommended, with v3.0.0 as the minimum. The v2.x line is legacy, and v3 requires WebGL 2 and removes the optimizeForTerrain option.

Source text

SKILL.md · MIT
---
name: mapbox-web-integration-patterns
description: 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.
---

# Mapbox Integration Patterns Skill

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):**

```bash
npm install mapbox-gl@^3.0.0    # Installs latest v3.x
```

**CDN (for prototyping only):**

```html
<!-- Replace VERSION with latest v3.x from https://docs.mapbox.com/mapbox-gl-js/ -->
<script src="https://api.mapbox.com/mapbox-gl-js/vVERSION/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/vVERSION/mapbox-gl.css" rel="stylesheet" />
```

### 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

```bash
npm install @mapbox/search-js-react@^1.0.0      # React
npm install @mapbox/search-js-web@^1.0.0        # Other frameworks
```

### Version Migration Notes (v2.x to v3.x)

- WebGL 2 now required
- `optimizeForTerrain` 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):**

```javascript
const token = import.meta.env.VITE_MAPBOX_ACCESS_TOKEN; // Use env vars in production

// Global token (works since v1.x)
mapboxgl.accessToken = token;
const map = new mapboxgl.Map({ container: '...' });

// Per-map token (preferred for multi-map setups)
const map = new mapboxgl.Map({
  accessToken: token,
  container: '...'
});
```

## Core Principles

**Every Mapbox GL JS integration must:**

1. Initialize the map in the correct lifecycle hook
2. Store map instance in component state (not recreate on every render)
3. **Always call `map.remove()` on cleanup** to prevent memory leaks
4. Handle token management securely (environment variables)
5. Import CSS: `import 'mapbox-gl/dist/mapbox-gl.css'`

## React Integration (Primary Pattern)

**Pattern: useRef + useEffect with cleanup**

> **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](references/token-management.md) for other bundlers.

```jsx
import { useRef, useEffect } from 'react';
import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';

function MapComponent() {
  const mapRef = useRef(null); // Store map instance
  const mapContainerRef = useRef(null); // Store DOM reference

  useEffect(() => {
    mapboxgl.accessToken = import.meta.env.VITE_MAPBOX_ACCESS_TOKEN;

    mapRef.current = new mapboxgl.Map({
      container: mapContainerRef.current,
      center: [-71.05953, 42.3629],
      zoom: 13
    });

    // CRITICAL: Cleanup to prevent memory leaks
    return () => {
      mapRef.current.remove();
    };
  }, []); // Empty dependency array = run once on mount

  return <div ref={mapContainerRef} style={{ height: '100vh' }} />;
}
```

**Key points:**

- Use `useRef` for both map instance and container
- Initialize in `useEffect` with empty deps `[]`
- **Always return cleanup function** that calls `map.remove()`
- Never initialize map in render (causes infinite loops)

### React + Search JS

```jsx
import { useRef, useEffect, useState } from 'react';
import mapboxgl from 'mapbox-gl';
import { SearchBox } from '@mapbox/search-js-react';
import 'mapbox-gl/dist/mapbox-gl.css';

const accessToken = import.meta.env.VITE_MAPBOX_ACCESS_TOKEN;
const center = [-71.05953, 42.3629];

function MapWithSearch() {
  const mapRef = useRef(null);
  const mapContainerRef = useRef(null);
  const [inputValue, setInputValue] = useState('');

  useEffect(() => {
    mapboxgl.accessToken = accessToken;

    mapRef.current = new mapboxgl.Map({
      container: mapContainerRef.current,
      center: center,
      zoom: 13
    });

    return () => {
      mapRef.current.remove();
    };
  }, []);

  return (
    <>
      <div
        style={{
          margin: '10px 10px 0 0',
          width: 300,
          right: 0,
          top: 0,
          position: 'absolute',
          zIndex: 10
        }}
      >
        <SearchBox
          accessToken={accessToken}
          map={mapRef.current}
          mapboxgl={mapboxgl}
          value={inputValue}
          proximity={center}
          onChange={(d) => setInputValue(d)}
          marker
        />
      </div>
      <div ref={mapContainerRef} style={{ height: '100vh' }} />
    </>
  );
}
```

## Search JS Integration Summary

**Install:**

```bash
npm install @mapbox/search-js-react      # React
npm install @mapbox/search-js-web        # Vanilla/Vue/Svelte
```

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
- `map`: Map instance (must be initialized first)
- `mapboxgl`: The mapboxgl library reference
- `proximity`: `[lng, lat]` to bias results geographically
- `marker`: Boolean to show/hide result marker
- `placeholder`: Search box placeholder text

### Positioning Search Box

**Absolute positioning (overlay):**

```jsx
<div
  style={{
    position: 'absolute',
    top: 10,
    right: 10,
    zIndex: 10,
    width: 300
  }}
>
  <SearchBox {...props} />
</div>
```

**Common positions:**

- Top-right: `top: 10px, right: 10px`
- Top-left: `top: 10px, left: 10px`
- Bottom-left: `bottom: 10px, left: 10px`

## Common Mistakes (Critical)

### Mistake 1: Forgetting to call map.remove()

```javascript
// BAD - Memory leak!
useEffect(() => {
  const map = new mapboxgl.Map({ ... })
  // No cleanup function
}, [])

// GOOD - Proper cleanup
useEffect(() => {
  const map = new mapboxgl.Map({ ... })
  return () => map.remove()  // Cleanup
}, [])
```

**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

```javascript
// BAD - Infinite loop in React!
function MapComponent() {
  const map = new mapboxgl.Map({ ... })  // Runs on every render
  return <div />
}

// GOOD - Initialize in effect
function MapComponent() {
  useEffect(() => {
    const map = new mapboxgl.Map({ ... })
  }, [])
  return <div />
}
```

**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

```javascript
// BAD - map variable lost between renders
function MapComponent() {
  useEffect(() => {
    let map = new mapboxgl.Map({ ... })
    // map variable is not accessible later
  }, [])
}

// GOOD - Store in useRef
function MapComponent() {
  const mapRef = useRef()
  useEffect(() => {
    mapRef.current = new mapboxgl.Map({ ... })
    // mapRef.current accessible throughout component
  }, [])
}
```

**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)

```javascript
// BAD - Vue's reactivity wraps data() objects in a Proxy, breaking mapbox-gl internals!
export default {
  data() {
    return {
      map: null  // Will be wrapped in a Proxy
    }
  },
  mounted() {
    this.map = new mapboxgl.Map({ ... })  // Proxy breaks GL internals
  }
}

// GOOD - Assign map as a plain instance property, not in data()
export default {
  mounted() {
    this.map = new mapboxgl.Map({
      container: this.$refs.mapContainer,
      center: [-71.05953, 42.3629],
      zoom: 13
    })
  },
  unmounted() {
    this.map?.remove()
  }
}
```

**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.

### Mistake 5: Silent style/tile failures (no error handler)

```javascript
// BAD — blank map when the token/style fails
const map = new mapboxgl.Map({ ... });

// GOOD — surface failures
map.on('error', (e) => {
  console.error(e.error || e);
  // optionally show an on-page error message
});
```

### Mistake 6: Broken deck.gl CDN via jsDelivr `+esm`

```html
<!-- BAD — often throws: does not provide export named 'makeBatchFromTable' -->
<script type="module">
  import { MapboxOverlay } from 'https://cdn.jsdelivr.net/npm/@deck.gl/[email protected]/+esm';
</script>

<!-- GOOD — UMD bundle (or esm.sh) -->
<script src="https://unpkg.com/[email protected]/dist.min.js"></script>
<script>
  const { MapboxOverlay, ScatterplotLayer } = deck;
  map.addControl(
    new MapboxOverlay({
      interleaved: false,
      layers: [
        /* ... */
      ]
    })
  );
</script>
```

Use `MapboxOverlay` (Mapbox IControl), not a bare `Deck` as a map control.

### Mistake 7: Draw toolbar without `draw.create`

If you load `mapbox-gl-draw`, listen for `draw.create` (and update the UI from `draw.getAll()`). Half-deleted handlers that leave a dangling `});` crash the page.

### Mistake 8: Layers lost after `setStyle` (no `style.load` rebind)

`map.setStyle(...)` replaces the style tree. Custom sources/layers/handlers added earlier are wiped unless you re-attach them.

```javascript
function onStyleReady() {
  // re-add sources, layers, and interaction handlers here
}

map.on('style.load', onStyleReady);

document.querySelectorAll('[data-style]').forEach((btn) => {
  btn.addEventListener('click', () => {
    map.setStyle(btn.dataset.style);
    // do NOT only add layers on the first 'load' — wait for style.load after every switch
  });
});
```

**Agent anti-pattern:** style switcher buttons that call `setStyle` once with no `style.load` rebind. The first style works; every switch after looks broken.

## Reference Files

Load these for framework-specific patterns and additional details:

- `references/vue.md` — Vue Integration (mounted/unmounted lifecycle)
- `references/svelte.md` — Svelte Integration (onMount/onDestroy)
- `references/angular.md` — Angular Integration with SSR handling
- `references/vanilla.md` — Vanilla JS (Vite) + Vanilla JS (CDN)
- `references/web-components.md` — Web Components (basic + reactive + usage in React/Vue/Svelte)
- `references/nextjs.md` — Next.js App Router + Pages Router
- `references/common-mistakes.md` — Common Mistakes 4-7 + Testing Patterns
- `references/token-management.md` — Token Management per bundler + Style Configuration

## When to Use This Skill

Invoke this skill when:

- Setting up Mapbox GL JS in a new project
- Integrating Mapbox into a specific framework (React, Vue, Svelte, Angular, Next.js)
- Building framework-agnostic Web Components
- Creating reusable map components for component libraries
- Debugging map initialization issues
- Adding Mapbox Search functionality
- Implementing proper cleanup and lifecycle management
- Converting between frameworks (e.g., React to Vue)
- Reviewing code for Mapbox integration best practices

## Related Skills

- **mapbox-cartography**: Map design principles and styling
- **mapbox-token-security**: Token management and security
- **mapbox-style-patterns**: Common map style patterns

## Resources

- [Mapbox GL JS Documentation](https://docs.mapbox.com/mapbox-gl-js/)
- [Mapbox Search JS Documentation](https://docs.mapbox.com/mapbox-search-js/)
- [create-web-app GitHub](https://github.com/mapbox/create-web-app)

© mapbox · MIT · mapbox/mapbox-agent-skills

Similar skills

Everything for Frontend and UI →

From the same set19

mapbox-web-performance-patterns Mapbox GL JS performance skill that eliminates initialization waterfalls, shrinks style bundles, and replaces sluggish DOM markers with GPU layers. CX Codex +1 2,500installs mapbox-style-patterns Ready-made Mapbox layer setups for common maps: POI finders, real estate, navigation, delivery. Useful when you want a proven style instead of picking colors from scratch. CX Codex +1 2,300installs mapbox-geospatial-operations A skill that helps your agent choose between Turf.js geometric math and Mapbox routing APIs. Useful when a task involves distances, zones or routes. CX Codex +1 2,200installs mapbox-cartography A skill that feeds your agent cartographic rules: layer hierarchy, color palettes, typography and zoom strategy for Mapbox styles. CX Codex +1 2,100installs mapbox-data-visualization-patterns Data visualization patterns for Mapbox GL JS in Claude Code, covering choropleth maps, heatmaps, and 3D styling. Helpful when plotting statistical data or point densities. CX Codex +1 2,100installs mapbox-style-quality A skill that walks your agent through Mapbox style QA: expression and GeoJSON validation, WCAG contrast checks, and style optimization. CX Codex +1 2,000installs mapbox-search-integration Walks your agent through the full Mapbox search rollout: discovery questions first, then a working integration with the right API and parameters. CX Codex +1 1,900installs mapbox-token-security Security guidelines for Mapbox access tokens: scope management, URL restrictions, and client initialization guards. CX Codex +1 1,900installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor