Labsco
mapbox logo

mapbox-web-integration-patterns

β˜… 68

by mapbox Β· part of mapbox/mapbox-agent-skills

Production-ready integration patterns for Mapbox GL JS across React, Vue, Svelte, Angular, and vanilla JavaScript. Covers framework-specific lifecycle management (useEffect/useRef in React, mounted/unmounted in Vue, onMount/onDestroy in Svelte, ngOnInit/ngOnDestroy in Angular) with proper cleanup to prevent memory leaks Includes Web Components pattern for framework-agnostic reusable map elements and cross-framework compatibility Provides token management via environment variables, Search JS...

πŸ”₯πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedFreeQuick setup
🧩 One of 7 skills in the mapbox/mapbox-agent-skills package β€” works on its own, and pairs well with its siblings.

Production-ready integration patterns for Mapbox GL JS across React, Vue, Svelte, Angular, and vanilla JavaScript. Covers framework-specific lifecycle management (useEffect/useRef in React, mounted/unmounted in Vue, onMount/onDestroy in Svelte, ngOnInit/ngOnDestroy in Angular) with proper cleanup to prevent memory leaks Includes Web Components pattern for framework-agnostic reusable map elements and cross-framework compatibility Provides token management via environment variables, Search JS...

Inspect the full instructions your agent will receiveExpand

This is the exact playbook injected into your agent when the skill activates β€” shown here so you can audit it before installing. You don't need to read it to use the skill.

by mapbox

Production-ready integration patterns for Mapbox GL JS across React, Vue, Svelte, Angular, and vanilla JavaScript. Covers framework-specific lifecycle management (useEffect/useRef in React, mounted/unmounted in Vue, onMount/onDestroy in Svelte, ngOnInit/ngOnDestroy in Angular) with proper cleanup to prevent memory leaks Includes Web Components pattern for framework-agnostic reusable map elements and cross-framework compatibility Provides token management via environment variables, Search JS... npx skills add https://github.com/mapbox/mapbox-agent-skills --skill mapbox-web-integration-patterns Download ZIPGitHub68

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.

Core Principles

Every Mapbox GL JS integration must:

  • 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

  • Handle token management securely (environment variables)

  • 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 for other bundlers.

Copy & paste β€” that's it
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 ;
}

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

Copy & paste β€” that's it
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 (
 <>
 
 setInputValue(d)}
 marker
 />
 

 
 
 );
}

Search JS Integration Summary

Install:

Copy & paste β€” that's it
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):

Copy & paste β€” that's it
 
 

Common positions:

  • Top-right: top: 10px, right: 10px

  • Top-left: top: 10px, left: 10px

  • Bottom-left: bottom: 10px, left: 10px

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