Polygon / Polyline: edits after a controlled `paths` update never reach `onPathsChanged`
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 82/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- react, typescript
- 領域
- frontend
調査の方向性
src/components/polygon.tsx のフック(~L168 リスナーエフェクト、~L244 setPaths 同期、~L266 エンコードされたパス)と src/components/polyline.tsx(~L154 リスナーエフェクト、~L191 setPath 同期)を読む。まず、issue の click-then-drag のシーケンスを再現するか、それらのコンポーネント周辺の既存テストを読んで、順序を確認する。完了の形は、両ファイルでリスナーエフェクトより前に同期エフェクトが宣言され、制御された更新後にリスナーが 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>
);
}
- Click the map 4 times. Each click is a controlled
pathsupdate. - 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, sosetPaths()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:
- The listener effect re-runs (it depends on
paths) and subscribes to the polygon's current inner MVCArrays. - 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分
- マージ済み PR(30日)
- 3
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
visgl/react-google-maps のほかの issue
-
google maps issue
難易度 3/5 1〜2日 初心者へのやさしさ 74/100
visgl/react-google-maps#1080 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug needs more info
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
visgl/react-google-maps#1039 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
visgl/react-google-maps#918 · コメント 9 件 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
visgl/react-google-maps#905 ·
メンテナーはふだん 1 日以内に返信
-
google maps issue
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
visgl/react-google-maps#889 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
visgl/react-google-maps の issue をすべて見る
似ている issue
-
accessibility area:ci ci-test level:low priority:high type:test
難易度 2/5 1〜3時間 初心者へのやさしさ 71/100
skytuhua-org/Gen-V1#3132 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
drmrd/vernacular#1238 ·
メンテナーはふだん 1 日以内に返信
-
question
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
enhancement migration-prioritized
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
Hudson-Pufferfish/local-party-console#136 ·
メンテナーはふだん 1 日以内に返信
-
priority:medium
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信