Skip to content

Commit 73c0974

Browse files
committed
fix: useMemo -> useState in useClusterer
1 parent be5f45f commit 73c0974

2 files changed

Lines changed: 57 additions & 16 deletions

File tree

example/src/Map.tsx

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useState, useRef } from 'react';
2-
import { Dimensions, StyleSheet, View } from 'react-native';
2+
import { Button, Dimensions, StyleSheet, View } from 'react-native';
33
import {
44
Clusterer,
55
isClusterFeature,
66
type Supercluster,
77
} from 'react-native-clusterer';
88
import MapView, { type Region } from 'react-native-maps';
9-
import { initialRegion, parsedPlacesData } from './places';
9+
import { getRandomData, initialRegion, parsedPlacesData } from './places';
1010
import { Point } from './Point';
1111

1212
type IFeature = Supercluster.PointOrClusterFeature<any, any>;
@@ -17,6 +17,9 @@ const MAP_DIMENSIONS = { width: MAP_WIDTH, height: MAP_HEIGHT };
1717

1818
export const Map = () => {
1919
const [region, setRegion] = useState<Region>(initialRegion);
20+
const [places, setPlaces] =
21+
useState<Supercluster.PointFeature<any>[]>(parsedPlacesData);
22+
const [options, setOptions] = useState({ radius: 18 });
2023
const mapRef = useRef<MapView>(null);
2124

2225
const _handlePointPress = (point: IFeature) => {
@@ -35,9 +38,9 @@ export const Map = () => {
3538
style={MAP_DIMENSIONS}
3639
>
3740
<Clusterer
38-
data={parsedPlacesData}
41+
data={places}
3942
region={region}
40-
options={{ radius: 18 }}
43+
options={options}
4144
mapDimensions={MAP_DIMENSIONS}
4245
renderItem={(item) => {
4346
return (
@@ -54,6 +57,18 @@ export const Map = () => {
5457
}}
5558
/>
5659
</MapView>
60+
<View style={styles.buttonContainer}>
61+
<Button
62+
title="Add 1000 points"
63+
onPress={() =>
64+
setPlaces(parsedPlacesData.concat(getRandomData(1000)))
65+
}
66+
/>
67+
<Button
68+
title="Change options"
69+
onPress={() => setOptions({ radius: Math.floor(Math.random() * 18) })}
70+
/>
71+
</View>
5772
</View>
5873
);
5974
};
@@ -62,4 +77,19 @@ const styles = StyleSheet.create({
6277
container: {
6378
flex: 1,
6479
},
80+
buttonContainer: {
81+
position: 'absolute',
82+
bottom: 0,
83+
left: 0,
84+
right: 0,
85+
flexDirection: 'row',
86+
justifyContent: 'space-between',
87+
width: '100%',
88+
backgroundColor: '#ed8e8efd',
89+
borderColor: '#ed8e8e',
90+
borderWidth: 1,
91+
borderRadius: 5,
92+
gap: 10,
93+
paddingHorizontal: 20,
94+
},
6595
});

src/useClusterer.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useMemo } from 'react';
1+
import { useEffect, useState } from 'react';
22
import type { Supercluster } from './types';
33
import type { MapDimensions, Region } from './types';
44
import SuperclusterClass from './Supercluster';
@@ -16,19 +16,30 @@ export function useClusterer<
1616
(Supercluster.PointFeature<P> | Supercluster.ClusterFeature<C>)[],
1717
SuperclusterClass<P, C>,
1818
] {
19-
const supercluster = useMemo(
20-
() => new SuperclusterClass(options).load(data),
21-
[data, options]
19+
const [supercluster, setSupercluster] = useState(
20+
new SuperclusterClass(options).load(data)
2221
);
2322

24-
const points = useMemo(
25-
() => supercluster.getClustersFromRegion(region, mapDimensions),
26-
[supercluster, region, mapDimensions]
23+
const [points, setPoints] = useState(
24+
supercluster.getClustersFromRegion(region, mapDimensions)
2725
);
2826

29-
// TODO: adjust type
30-
return [
31-
points as (Supercluster.PointFeature<P> | Supercluster.ClusterFeature<C>)[],
32-
supercluster,
33-
];
27+
useEffect(() => {
28+
setSupercluster(new SuperclusterClass(options).load(data));
29+
// Keep option properties as individual dependencies in case "options" prop is passed as an inline object
30+
// eslint-disable-next-line react-hooks/exhaustive-deps
31+
}, [
32+
data,
33+
options?.extent,
34+
options?.radius,
35+
options?.minPoints,
36+
options?.minZoom,
37+
options?.maxZoom,
38+
]);
39+
40+
useEffect(() => {
41+
setPoints(supercluster.getClustersFromRegion(region, mapDimensions));
42+
}, [supercluster, region, mapDimensions]);
43+
44+
return [points, supercluster];
3445
}

0 commit comments

Comments
 (0)