Polygon / Polyline: edits after a controlled `paths` update never reach `onPathsChanged`

未关闭 适合新手
#1,120 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@anubhavlal07 已经在做这个了。

开始于 2026年11月24日。

  • #1122 来自 @anubhavlal07 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
82/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
react, typescript
领域
frontend

调研方向

阅读 src/components/polygon.tsx 中的 hooks(~L168 listener effect、~L244 setPaths 同步、~L266 编码路径)和 src/components/polyline.tsx(~L154 listener effect、~L191 setPath 同步)。首先通过复现 issue 中的 click-then-drag 序列,或阅读这些组件周围的现有测试,来确认顺序。完成的样子是:两个文件中同步 effect 都在 listener effect 之前声明,listener 在受控更新后收到 insert_at/set_at/remove_at,并且现有测试套件没有回归。

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

描述

Version: @vis.gl/react-google-maps 1.10.2 (polygon.tsx and polyline.tsx are unchanged on main)

Description

After the paths prop of an editable <Polygon> changes in a way that makes the component call polygon.setPaths(), user edits no longer reach onPathsChanged. This lasts until a later render passes a new paths reference that equals the polygon's current path. The map shows the edit, but the parent's state never gets it. The next controlled update then overwrites the edit on the map too.

<Polyline> behaves the same way with path / onPathChanged.

Steps to reproduce
function Editor() {
  const [path, setPath] = useState<google.maps.LatLngLiteral[]>([]);

  return (
    <Map
      defaultCenter={{lat: 35, lng: 139}}
      defaultZoom={14}
      onClick={e => e.detail.latLng && setPath(p => [...p, e.detail.latLng!])}>
      <Polygon
        paths={path}
        editable
        onPathsChanged={paths => {
          console.log('onPathsChanged', paths[0].length);
          setPath(paths[0].map(c => c.toJSON()));
        }}
      />
    </Map>
  );
}
  1. Click the map 4 times. Each click is a controlled paths update.
  2. Drag a side's midpoint to insert a vertex, then drag another one.

Expected: onPathsChanged logs 5, then 6.
Actual: nothing is logged. The map shows 6 vertices, while the state still has 4.

What I observed:

  • If the parent re-sends the same points as a new array (setPath(p => [...p])) before editing, both edits are reported (5, then 6).
  • If the parent stores rounded coordinates (Number(lat.toFixed(7))), only the first of those edits is reported. The rounded value differs from the polygon's own, so setPaths() runs again and the listeners go stale again.
  • The same scenario with <Polyline path onPathChanged> gives the same results.
Cause

In usePolygon, the effect that subscribes to the MVCArrays (insert_at/set_at/remove_at, polygon.tsx ~L168) is declared before the effect that syncs the controlled paths (~L244). On a commit where paths differs from the polygon, the effects run in declaration order:

  1. The listener effect re-runs (it depends on paths) and subscribes to the polygon's current inner MVCArrays.
  2. The sync effect calls polygon.setPaths(paths), which replaces those arrays.

The listeners stay attached to arrays the polygon no longer uses. Nothing re-subscribes until a later render passes a new paths that doesn't trigger setPaths(). The encoded-paths effect (~L266) has the same ordering. usePolyline has the same ordering too: listener effect ~L154, setPath sync ~L191.

Suggested fix

Declare the paths / encodedPaths sync effects before the listener effect, in both hooks, so the listeners subscribe to the arrays that setPaths() / setPath() just created. I checked this with a minimal copy of the hook in that order. All edits were reported, including with rounded coordinates.

Another option is to re-subscribe right after setPaths() inside the sync effect.

Workaround

Leave onPathsChanged unset and listen to polygon.getPath() yourself, in an effect that runs after the component's effects. Note that draggable defaults to Boolean(onPathsChanged) (or a drag handler), so without the callback the polygon is no longer draggable unless draggable is set explicitly.

主要语言
TypeScript
星标
2k
派生
195
平均合并
6 小时 26 分钟
30 天内合并 PR
3

环境准备

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

从这里开始

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

visgl/react-google-maps 的其他 Issue

查看 visgl/react-google-maps 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

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