Overlay collection hooks
Keep lists of markers, polygons, lines and circles in state, and render them declaratively
These hooks keep a list of overlays in React state with stable IDs, so you can add, update and remove them from anywhere and render them with the matching components.
useMarkers
import { Marker, SphereMap, useMarkers } from "gistda-sphere-react";
function Stores() {
const { items, add, remove } = useMarkers();
return (
<>
<button
onClick={() => add({ position: { lon: 100.5, lat: 13.75 }, title: "New store" })}
>
Add store
</button>
<SphereMap>
{items.map((marker) => (
<Marker
key={marker.id}
position={marker.position}
title={marker.title}
onClick={() => remove(marker.id)}
/>
))}
</SphereMap>
</>
);
}usePolygons, usePolylines and useCircles
These work the same way for Polygon (positions), Polyline (positions) and Circle (center, radius) data.
useOverlays
All four collections at once:
import { useOverlays } from "gistda-sphere-react";
const { markers, polygons, polylines, circles, clearAll } = useOverlays();Return value
Each collection hook returns:
| Property | Type | Description |
|---|---|---|
items | T[] | The overlays, each with an id. |
add(data) | string | Add an overlay and get its new ID. |
update(id, changes) | Merge changes into an overlay. | |
remove(id) | Remove an overlay. | |
clear() | Remove all overlays of this type. | |
get(id) | T | undefined | Find an overlay by ID. |
The data objects accept the same props as the components (title, fillColor, draggable…), so you can spread them: <Polygon key={p.id} {...p} />.