Issues with useControl when upgrading to nextjs 14.2

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

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
领域
frontend, web-dev

调研方向

从 draw-control.tsx 中的 useControl 回调以及调用 draw.getAll() 的 useEffect 开始;使用报告中的 Mapbox Draw 设置比较 Next 14.1 和 14.2 下的行为。一个有用的复现或测试应当确定为什么地图加载后 getAll 不可用,其中 done 意味着能够可靠地访问当前绘制内容。

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

描述

bug
Description

I'm using an implementation that can be summed up as the following

<Map
        initialViewState={initialViewState}
        style={{ width: '100%', height: '100%' }}
        mapStyle="mapbox://styles/mapbox/satellite-streets-v12"
      >
        <DrawControl
          position="top-left"
          onCreate={onUpdate}
          onUpdate={onUpdate}
          onDelete={onDelete}
        />
</Map>

Where DrawControl looks something like...

import MapboxDraw from '@mapbox/mapbox-gl-draw';
import { useControl } from 'react-map-gl';

export default function DrawControl({
  onCreate = () => {},
  onUpdate = () => {},
  onDelete = () => {},
  ...props
}: DrawControlProps) {
  const [draw, setDraw] = useState<MapboxDraw | null>(null);
  useControl<MapboxDraw>(
    () => {
      const drawInstance = new MapboxDraw(props);
      setDraw(drawInstance);
      return drawInstance;
    },
    ({ map }) => {
      map.on('draw.create', onCreate);
      map.on('draw.update', onUpdate);
      map.on('draw.delete', onDelete);
    },
    ({ map }) => {
      map.off('draw.create', onCreate);
      map.off('draw.update', onUpdate);
      map.off('draw.delete', onDelete);
    },
    {
      position: props.position,
    }
  );

  useEffect(() => {
    if (!draw || !mapReady) return;
    const currentDrawings = draw.getAll();
}, [draw, someOtherVariableWeTrack]

We haven't had any issues with this implementation until upgrading to next 14.2. I've downgraded back to next 14.1 and confirmed that the issue is selective to next 14.2 for reasons I'm still trying to understand. The error I get is that draw.getAll() is undefined. Upon debugging, I can see that draw is indeed defined, along with some non-function attributes, but all the functions yield the same undefined error.

I have confirmed that this behavior happens even after the map is definitively loaded. I now have high confidence the issue is with the way I am storing drawInstance. From other posts online, it seems like this issue happens when the map is not yet loaded. However, I can confirm that the map is loaded and the draw control is present -- I can draw polygons on the map with no issues, but whenever I hit this useEffect, I always get the error, even once the map is loaded and I can confirm the ability to draw.

This is what leads me to believe it is something related to the way useControl is working, or at the very least is something related to the way that we're referencing the drawInstance.

Expected Behavior

In next 14.1, this works as expected. The expected behavior is to have access to the drawings that are currently on the map.

Steps to Reproduce

Can provide codesandbox if the description is not enough to understand the issue. Please let me know.

Environment
Logs
TypeError: Cannot read properties of undefined (reading 'getAll')
    at e.getAll (mapbox-gl-draw.js:1:77757)
    at eval (draw-control.tsx:50:34)
    at commitHookEffectListMount (react-dom.development.js:21102:1)
    at commitHookPassiveMountEffects (react-dom.development.js:23154:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23259:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23334:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23370:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
    at commitPassiveMountOnFiber (react-dom.development.js:23256:1)
    at recursivelyTraversePassiveMountEffects (react-dom.development.js:23237:1)
主要语言
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 摘要。