Polygon / Polyline: edits after a controlled `paths` update never reach `onPathsChanged`
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 82/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- react, typescript
- Lĩnh vực
- frontend
Hướng nghiên cứu
Đọc các hook trong src/components/polygon.tsx (~L168 hiệu ứng listener, ~L244 đồng bộ setPaths, ~L266 đường dẫn đã mã hóa) và src/components/polyline.tsx (~L154 hiệu ứng listener, ~L191 đồng bộ setPath). Trước tiên xác nhận thứ tự bằng cách tái hiện chuỗi click-rồi-kéo từ issue, hoặc bằng cách đọc các test hiện có xung quanh các component đó. Hoàn tất trông như sau: các hiệu ứng đồng bộ được khai báo trước hiệu ứng listener trong cả hai file, listener nhận insert_at/set_at/remove_at sau một cập nhật có kiểm soát, và không có hồi quy trong bộ test hiện có.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- TypeScript
- Star
- 2k
- Fork
- 195
- Merge trung bình
- 6 giờ 26 phút
- Pull request đã merge (30 ngày)
- 3
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của visgl/react-google-maps
-
google maps issue
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 74/100
visgl/react-google-maps#1080 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug needs more info
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
visgl/react-google-maps#1039 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
visgl/react-google-maps#918 · 9 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
visgl/react-google-maps#905 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
google maps issue
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
visgl/react-google-maps#889 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của visgl/react-google-maps
Issue tương tự
-
accessibility area:ci ci-test level:low priority:high type:test
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 71/100
skytuhua-org/Gen-V1#3132 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
drmrd/vernacular#1238 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
question
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
enhancement migration-prioritized
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Hudson-Pufferfish/local-party-console#136 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
priority:medium
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày