@adamscybot/react-leaflet-component-marker

中文文档
2.0.3发布于 2025/03/10MITTypeScriptES Module近 30 天下载 1.5万

A tiny wrapper for react-leaflet's component that allows you to use a React component as a marker, with working state, handlers, and access to parent contexts.

原始使用文档
文档目录3 节
  1. Simple Usage
  2. Example
  3. Advanced Usage

react-leaflet-component-marker

👉 DEMO

📍 Use a React component as React Leaflet markers.
🔄 Familiar swap-in API that feels like React Leaflet.
✨ Can use state, context etc. It's a full component. No BS.
💪 It's strongly typed.

What is it

A tiny wrapper for react-leaflet's <Marker /> component that allows you to use a React component as a marker, with working state, handlers, and access to parent contexts.

The approach this library uses differs from other approaches that use renderToString in that it instead uses React's Portal functionality to achieve the effect. That means the component is not static, but a full first-class component that can have its own state, event handlers & lifecycle.

I struggled to find something that worked in a way where I could simply drop something in from a design system, and have all the context available such that it works, as well as all the interactions working as they should.

Many existing packages exist but they use techniques that mean they are very limited.

Installation

Install using your projects package manager.

NPM

npm install --save @adamscybot/react-leaflet-component-marker

Yarn

yarn install --save @adamscybot/react-leaflet-component-marker

PNPM

pnpm add @adamscybot/react-leaflet-component-marker

Docs

Simple Usage

Instead of importing Marker from react-leaflet, import Marker from @adamscybot/react-leaflet-component-marker.

The icon prop is extended to allow for a JSX element of your choosing. All other props are identical to the react-leaflet Marker API, but there is an additional prop called componentIconOpts for Advanced Usage.

The icon prop can also accept all of the original types of icons that the underlying react-leaflet Marker accepts. Though there is no gain in using this library for this case, it may help if you want to just use this library in place of Marker universally.

Example

import React from 'react'
import { MapContainer, TileLayer } from 'react-leaflet'
import { Marker } from '@adamscybot/react-leaflet-component-marker'
import 'leaflet/dist/leaflet.css'

const MarkerIconExample = () => {
  return (
    <>
      <button onClick={() => console.log('button 1 clicked')}>Button 1</button>
      <button onClick={() => console.log('button 2 clicked')}>Button 2</button>
    </>
  )
}

const CENTER = [51.505, -0.091]
const ZOOM = 13
const App = () => {
  return (
    <MapContainer center={CENTER} zoom={ZOOM}>
      <TileLayer
        attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        url="https://{s}.tile.osm.org/{z}/{x}/{y}.png"
      />
      <Marker position={CENTER} icon={<MarkerIconExample />} />
    </MapContainer>
  )
}

Advanced Usage

The componentIconOpts prop can be passed, which is an object with additional options for more advanced use cases. Note, in the case where you are not passing a component to icon, these settings will be ignored.

Below is a list of properties this object can be provided.

layoutMode

The layoutMode controls how the bounding box of the React component marker behaves. It accepts two options:

  • fit-content (default). In this mode, the React component itself defines the dimensions of the marker. The component can shrink and expand at will. Logic internally to this library centers the component on its coordinates to match Leaflets default positioning; however, Leaflet itself is effectively no longer in control of this.
  • fit-parent. In this mode, the dimensions of the React component marker are bound by the iconSize passed to componentIconOpts.rootDivOpts. Leaflet is therefore in control of the dimensions and positioning. Component markers should use elements with 100% width & height to fill the available size if needed.

rootDivOpts

[!NOTE] Some options are not supported since they do not apply or make sense in the case of a React component marker. The unsupported options are html, bgPos, shadowUrl, shadowSize, shadowAnchor, shadowRetinaUrl, iconUrl and iconRetinaUrl.

An object containing properties from the supported subset of the underlying Leaflet divIcon options, which this library uses as a containing wrapper.

If using fit-parent, you must set iconSize here.

disableScrollPropagation

false by default.

If set to true, panning/scrolling the map will not be possible "through" the component marker.

disableClickPropagation

false by default.

If set to true, clicking on the component marker will not be captured by the underlying map.

unusedOptsWarning

true by default.

Can be set to false in order to not warn in console about cases where componentIconOpts was set but icon was not a React component.

unusedOptsWarning

true by default.

Can be set to false in order to not warn in console about cases where the layoutMode was fit-parent but their was no iconSize defined in the rootDivOpts.

版本与发布记录

@adamscybot/react-leaflet-component-marker 共发布 6 个版本,首次发布于 2024/02/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.3latest2025/03/101.7 年前
2.0.22024/12/241.9 年前
2.0.12024/08/242 年前
2.0.02024/07/122 年前
1.1.02024/02/042 年前
1.0.22024/02/042 年前

关于 @adamscybot/react-leaflet-component-marker 的常见问题

@adamscybot/react-leaflet-component-marker 是什么?

A tiny wrapper for react-leaflet's component that allows you to use a React component as a marker, with working state, handlers, and access to parent contexts.

如何安装 @adamscybot/react-leaflet-component-marker?

在项目目录运行 npm install @adamscybot/react-leaflet-component-marker,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@react-leaflet/core(>=2.0.0)、leaflet(>=1.9.0)、react(>=18.0.0)、react-dom(>=18.0.0)、react-leaflet(>=4.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@adamscybot/react-leaflet-component-marker 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@adamscybot/react-leaflet-component-marker 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@adamscybot/react-leaflet-component-marker 可以商用吗?

@adamscybot/react-leaflet-component-marker 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@adamscybot/react-leaflet-component-marker 的下载量有多少?

根据 npm 官方下载统计,@adamscybot/react-leaflet-component-marker 在 2026/10/05 – 2026/11/03 这 30 天内被下载 14,630 次,日均约 488 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@adamscybot/react-leaflet-component-marker 还在维护吗?

最新版本 2.0.3 发布于 2025/03/10(1.7 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/02/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。