When changes occur to line geojson sourced from state, map does not update #2418
Unanswered
brianbancroft
asked this question in
Q&A
Replies: 1 comment
-
When adding an id attribute to the geojson feature, and changing it on feature change, the map will update on a press of the "append" button. const appendData = () => {
const sourceData = { ...data };
const deltaX = -0.01;
const deltaY =
0.001 * (sourceData.geometry.coordinates.length % 2 === 0 ? 1 : -1);
const newCoordinates = [
sourceData.geometry.coordinates[
sourceData.geometry.coordinates.length - 1
][0] + deltaX,
sourceData.geometry.coordinates[0][1] + deltaY,
];
sourceData.geometry.coordinates.push(newCoordinates);
sourceData.id = String(Number(sourceData.id) + 1);
setData(sourceData);
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Duplicate of #2366, but with example and reproduceability.
In this example, you have a map with two buttons: "toggle" and "append". Append adds a vertex to a line geojson-compliant object stored in React.useState:
Clicking on "append" adds a vertex in a westward sawtooth pattern to the line geojson object, however the map fails to update. Clicking on "toggle" swaps the geojson object with an entirely separate object, and successfully updates the map. Clicking on toggle twice shows the sawtooth with the added vertices.
Example: https://codesandbox.io/p/sandbox/react-map-gl-update-geojson-2rtvdm?file=%2Fsrc%2FSawtoothMap.jsx
The basemap fails to render in this codesandbox on account of token 403-errors, but shape will render when the user clicks on the buttons as displayed in the video below:
Screen.Recording.2024-07-18.at.1.49.53.PM.mov
Is there an easy solution to this problem I'm missing?
Beta Was this translation helpful? Give feedback.
All reactions