npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-mapbox-gl

v2.2.2

Published

mapbox-gl react hook

Readme

use-mapbox-gl

Simple, 0 dependency hook around mapbox-gl

NPM Conventional Commits

🖥 Demo

Check out the demo

📦 Installation

with npm
$ npm install --save use-mapbox-gl
with yarn
$ yarn add use-mapbox-gl

⚠️ Don't forget install peer dependencies! If it not alredy installed

with npm
$ npm install --save mapbox-gl
with yarn
$ yarn add mapbox-gl

💅 Import styles. You also need to use mapbox-gl styles

If you are using create-react-app add this link to the public/index.html into the head tag

<link
  href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css"
  rel="stylesheet"
/>

OR You can also import styles from mapbox-gl dependencies

Add this import to your src/index.js

import "mapbox-gl/dist/index.css"

📖 Examples

🔗 With token

import React from "react";
import { useMapboxGl } from "use-mapbox-gl";

function BasicMap() {
  const containerRef = useRef();

  useMapboxGl(containerRef, {
    style: "mapbox://styles/mapbox/streets-v11",
    accessToken: "your_access_token",
  });

  return <div ref={containerRef} />;
}

export default BasicMap

🔗 Without token

For using without token, you need to define custom base map style, as example:

import React from "react";
import { useMapboxGl } from "use-mapbox-gl";

const osmStyle = {
  version: 8,
  sources: {
    osm: {
      type: "raster",
      tiles: [
        "https://a.tile.openstreetmap.org/{z}/{x}/{y}.png",
        "https://b.tile.openstreetmap.org/{z}/{x}/{y}.png",
        "https://c.tile.openstreetmap.org/{z}/{x}/{y}.png",
      ],
      tileSize: 256,
    },
  },
  layers: [
    {
      id: "osm",
      source: "osm",
      type: "raster",
    },
  ],
};

function WithoutTokenMap() {
  const containerRef = useRef();

  useMapboxGl(containerRef, {
    style: osmStyle,
    zoom: 9,
    center: [30, 50],
  });

  return <div ref={containerRef} />;
}

export default WithoutTokenMap

🕹 API

🔗 useMapboxGl

  • container - The HTML element React ref in which Mapbox GL JS will render the map
  • options (optional) - object with native mapbox-gl parameters, without container prop
  • setMapApi (optional) - map load callback, called when the [mapbox-gl load event] (https://docs.mapbox.com/mapbox-gl-js/api/map/#map.event:load) occurs
  • cleanMapApi (optional) - map cleanup callback, called when the map container is unmounted
useMapboxGl(
  container: React.MutableRefObject<HTMLElement> 
  options?: Omit<MapboxOptions, "container">
  setMapApi?: (map: mapboxgl.Map) => void 
  cleanMapAPI?: () => void
)

License

MIT © dqunbp