gistda-sphere-react

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.

0 markers

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:

PropertyTypeDescription
itemsT[]The overlays, each with an id.
add(data)stringAdd 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 | undefinedFind 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} />.

On this page