[Bug] Marker crashes with "appendChild" error during rapid client-side navigation (React 19 / Next.js 16)

未关闭
#2,584 1 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

  • #2585 来自 @officialasishkumar —— 已关闭,未合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清

调研方向

从 modules/react-mapbox/src/components/marker.ts 开始,重点查看相关行附近的 useEffect,并复现 issue 中描述的快速客户端导航。根据 Marker 的生命周期和清理行为检查建议的处理方式。完成的标准是:快速导航不再产生 appendChild 崩溃,同时不破坏 Marker 的正常挂载和移除。

由索引模型根据 Issue 内容生成。

描述

bug
Description

The Marker component crashes with Cannot read properties of undefined (reading 'appendChild') during rapid client-side navigation (e.g., quickly navigating between a random page and a page that contains a <Map> with <Marker> children).

Expected Behavior

No response

Steps to Reproduce

TL:DR steps to reproduce:

Quickly navigate back and forth between pages which contain a <Map> with <Marker> children.

Steps to Reproduce

  1. Create a Next.js 16 app with React 19
  2. Page A: a list page (no map)
  3. Page B: a detail page with containing children
  4. Navigate rapidly between Page A and Page B using client-side navigation (clicking links, not full page loads)
  5. After a few rapid back-and-forth navigations, the error appears

Root Cause

The Marker component's useEffect at marker.ts#L86 calls marker.addTo(map.getMap()) with [] deps. During rapid unmount/remount cycles in React 19, the new mount's useEffect can fire before the previous mount's cleanup has run. At that point, mapbox-gl's Map instance still exists but its internal DOM container sub-elements have been torn down, causing marker.addTo() to fail when it tries to appendChild into a destroyed container.

Through instrumentation, I confirmed:

  • map.getMap() returns a valid Map instance (truthy)
  • map._container exists (truthy)
  • But inside marker.addTo(), deeper container child elements are undefined
  • The error is intermittent and depends on navigation speed

Suggested Fix

Wrap marker.addTo(map.getMap()) in a try-catch in the useEffect

useEffect(() => {
    try {
        marker.addTo(map.getMap());
    } catch {
        // Map container was destroyed during rapid navigation — safe to ignore
        return;
    }
    return () => {
        marker.remove();
    };
}, []);
Environment
  • react-map-gl: 8.1.0 (via @vis.gl/react-mapbox)
  • mapbox-gl: 3.11.0
  • React: 19.1.0
  • Next.js: 16.2.2 (App Router, Turbopack)
  • Browser: Chrome
Logs

No response

主要语言
TypeScript
星标
8.5k
派生
1.3k
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

visgl/react-map-gl 的其他 Issue

查看 visgl/react-map-gl 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。