Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion modules/react-mapbox/src/components/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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 <Source> prop: ${key}`);
}
}
} else if (type === 'image') {
(source as ImageSourceImplementation).updateImage({
url: props.url,
Expand Down
52 changes: 51 additions & 1 deletion modules/react-mapbox/test/components/source.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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(
<Map
ref={mapRef}
mapLib={import('mapbox-gl-v3')}
mapStyle={mapStyle}
mapboxAccessToken={MapboxAccessToken}
>
<Source id="my-data" type="geojson" data={emptyData} tolerance={0.375} />
</Map>
)
);
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(
<Map
ref={mapRef}
mapLib={import('mapbox-gl-v3')}
mapStyle={mapStyle}
mapboxAccessToken={MapboxAccessToken}
>
<Source id="my-data" type="geojson" data={emptyData} tolerance={1} />
</Map>
)
);
expect(source._data, 'Data set with setData is kept').toEqual(pushedData);
expect(warn, 'Option that cannot be applied is reported').toHaveBeenCalledWith(
'Unable to update <Source> prop: tolerance'
);
warn.mockRestore();

await act(() => root.unmount());
});
12 changes: 11 additions & 1 deletion modules/react-maplibre/src/components/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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 <Source> prop: ${key}`);
}
}
} else if (type === 'image') {
(source as ImageSourceImplementation).updateImage({
url: props.url,
Expand Down
42 changes: 41 additions & 1 deletion modules/react-maplibre/test/components/source.spec.jsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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(
<Map ref={mapRef} mapStyle={mapStyle}>
<Source id="my-data" type="geojson" data={emptyData} tolerance={0.375} />
</Map>
)
);
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(
<Map ref={mapRef} mapStyle={mapStyle}>
<Source id="my-data" type="geojson" data={emptyData} tolerance={1} />
</Map>
)
);
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 <Source> prop: tolerance'
);
warn.mockRestore();

await act(() => root.unmount());
});