diff --git a/modules/react-mapbox/src/components/source.ts b/modules/react-mapbox/src/components/source.ts index 2e02c9740..2af7bc33e 100644 --- a/modules/react-mapbox/src/components/source.ts +++ b/modules/react-mapbox/src/components/source.ts @@ -45,11 +45,13 @@ function updateSource(source: AnySourceImplementation, props: SourceProps, prevP let changedKey = ''; let changedKeyCount = 0; + const changedKeys: string[] = []; for (const key in props) { if (key !== 'children' && key !== 'id' && !deepEqual(prevProps[key], props[key])) { changedKey = key; changedKeyCount++; + changedKeys.push(key); } } @@ -60,7 +62,15 @@ function updateSource(source: AnySourceImplementation, props: SourceProps, prevP const type = props.type; if (type === 'geojson') { - (source as GeoJSONSourceImplementation).setData(props.data); + // Only send data when it changed, it may have been set imperatively + for (const key of changedKeys) { + if (key === 'data') { + (source as GeoJSONSourceImplementation).setData(props.data); + } else { + // eslint-disable-next-line + console.warn(`Unable to update prop: ${key}`); + } + } } else if (type === 'image') { (source as ImageSourceImplementation).updateImage({ url: props.url, diff --git a/modules/react-mapbox/test/components/source.spec.jsx b/modules/react-mapbox/test/components/source.spec.jsx index baa8b8024..a97119534 100644 --- a/modules/react-mapbox/test/components/source.spec.jsx +++ b/modules/react-mapbox/test/components/source.spec.jsx @@ -2,7 +2,7 @@ import {Map, Source} from '@vis.gl/react-mapbox'; import * as React from 'react'; import {createRoot} from 'react-dom/client'; import {act} from 'react-dom/test-utils'; -import {expect, test} from 'vitest'; +import {expect, test, vi} from 'vitest'; import {waitForMapLoad, waitForMapStyleLoad, actUntil} from '../utils/test-utils'; import {MapboxAccessToken} from '../utils/token'; @@ -75,3 +75,53 @@ test('Source/Layer', async () => { await act(() => root.unmount()); }); + +test('Source/geojson keeps data set imperatively when another prop changes', async () => { + const root = createRoot(document.createElement('div')); + const mapRef = {current: null}; + + const mapStyle = {version: 8, sources: {}, layers: []}; + const emptyData = {type: 'FeatureCollection', features: []}; + const pushedData = { + type: 'FeatureCollection', + features: [{type: 'Feature', geometry: {type: 'Point', coordinates: [0, 0]}, properties: {}}] + }; + + await act(() => + root.render( + + + + ) + ); + await waitForMapLoad(mapRef); + const source = mapRef.current.getSource('my-data'); + expect(source, 'Source is added').toBeTruthy(); + source.setData(pushedData); + + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + await act(() => + root.render( + + + + ) + ); + expect(source._data, 'Data set with setData is kept').toEqual(pushedData); + expect(warn, 'Option that cannot be applied is reported').toHaveBeenCalledWith( + 'Unable to update prop: tolerance' + ); + warn.mockRestore(); + + await act(() => root.unmount()); +}); diff --git a/modules/react-maplibre/src/components/source.ts b/modules/react-maplibre/src/components/source.ts index 1a704a377..9d8e93e1c 100644 --- a/modules/react-maplibre/src/components/source.ts +++ b/modules/react-maplibre/src/components/source.ts @@ -39,11 +39,13 @@ function updateSource(source: AnySourceImplementation, props: SourceProps, prevP let changedKey = ''; let changedKeyCount = 0; + const changedKeys: string[] = []; for (const key in props) { if (key !== 'children' && key !== 'id' && !deepEqual(prevProps[key], props[key])) { changedKey = key; changedKeyCount++; + changedKeys.push(key); } } @@ -54,7 +56,15 @@ function updateSource(source: AnySourceImplementation, props: SourceProps, prevP const type = props.type; if (type === 'geojson') { - (source as GeoJSONSourceImplementation).setData(props.data); + // Only send data when it changed, it may have been set imperatively + for (const key of changedKeys) { + if (key === 'data') { + (source as GeoJSONSourceImplementation).setData(props.data); + } else { + // eslint-disable-next-line + console.warn(`Unable to update prop: ${key}`); + } + } } else if (type === 'image') { (source as ImageSourceImplementation).updateImage({ url: props.url, diff --git a/modules/react-maplibre/test/components/source.spec.jsx b/modules/react-maplibre/test/components/source.spec.jsx index 623a025f3..0216670d0 100644 --- a/modules/react-maplibre/test/components/source.spec.jsx +++ b/modules/react-maplibre/test/components/source.spec.jsx @@ -1,5 +1,5 @@ /* global document */ -import {expect, test} from 'vitest'; +import {expect, test, vi} from 'vitest'; import * as React from 'react'; import {createRoot} from 'react-dom/client'; import {act} from 'react-dom/test-utils'; @@ -56,3 +56,43 @@ test('Source/Layer', async () => { await act(() => root.unmount()); }); + +test('Source/geojson keeps data set imperatively when another prop changes', async () => { + const root = createRoot(document.createElement('div')); + const mapRef = {current: null}; + + const mapStyle = {version: 8, sources: {}, layers: []}; + const emptyData = {type: 'FeatureCollection', features: []}; + const pushedData = { + type: 'FeatureCollection', + features: [{type: 'Feature', geometry: {type: 'Point', coordinates: [0, 0]}, properties: {}}] + }; + + await act(() => + root.render( + + + + ) + ); + await waitForMapLoad(mapRef); + const source = mapRef.current.getSource('my-data'); + expect(source, 'Source is added').toBeTruthy(); + source.setData(pushedData); + + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}); + await act(() => + root.render( + + + + ) + ); + expect(await source.getData(), 'Data set with setData is kept').toEqual(pushedData); + expect(warn, 'Option that cannot be applied is reported').toHaveBeenCalledWith( + 'Unable to update prop: tolerance' + ); + warn.mockRestore(); + + await act(() => root.unmount()); +});