• Stars
    star
    134
  • Rank 270,967 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A React Hook for using Supercluster

useSupercluster

A hook for using Supercluster with React.

const { clusters, supercluster } = useSupercluster({
  points: [],
  bounds: [
    -1.2411810957931664,
    52.61208435908725,
    -1.0083656811012531,
    52.64495957533833
  ],
  zoom: 12,
  options: { radius: 75, maxZoom: 20 }
});

Installation

You will need to install Supercluster as a peer dependency of this package.

yarn add supercluster use-supercluster

Examples

This package contains an example along with tests, but full examples with instructions in the most popular mapping libraries for React can be found below.

Mapbox

Full instructions and an example can be found here.

Google Maps

Full instructions and an example can be found here.

Leaflet

Full instructions and an example can be found here.

Configuration

The last (fourth) argument passed to the useSupercluster hook are options that are passed directly to the instance of Supercluster. You can use any of Supercluster's options.

Map & Reduce Options

As an example, you can use map and reduce to keep track of a total value summed up from across the points. In this case we have the total cost, the max severity, plus another count (which is redundant because Supercluster gives us the point_count property).

const options = {
  radius: 75,
  maxZoom: 20,
  map: props => ({
    cost: props.cost,
    severity: props.severity,
    count: 1
  }),
  reduce: (acc, props) => {
    acc.count += 1;
    acc.cost += props.cost;
    acc.severity = Math.max(acc.severity, props.severity);
    return acc;
  }
};

I found map and reduce a little confusing! The value returned from map of the first point is used as the initial value passed as the accumulator to the reduce function. The only props you have available in reduce are the ones returned from map. You technically don't need to return a value from reduce (it's not used), but instead need to mutate the accumulator object.

Then these accumulated properties can be used and are available on each cluster:

<ul>
  {clusters.map(point => {
    const properties = point.properties || {};
    if (properties.cluster) {
      return (
        <li key={point.id}>
          <h2>Points: {properties.point_count}</h2>
          <p>Cost: {properties.cost.toFixed(2)}</p>
          <p>Severity: {properties.severity}</p>
          <p>Count: {properties.count}</p>
        </li>
      );
    } else {
      return <li key={properties.crimeId}>{properties.category}</li>;
    }
  })}
</ul>

More Repositories

1

google-maps-react-2020

JavaScript
108
star
2

mapbox-react-demo

Mapbox in React demo to create an interactive map
JavaScript
89
star
3

house-course

TypeScript
82
star
4

google-maps-react-crash-course

TypeScript
56
star
5

google-maps-react-demo

Google Maps in React Demo
JavaScript
54
star
6

nextjs-graphql-example

JavaScript
53
star
7

react-leaflet-demo

JavaScript
52
star
8

apollo-generating-types

Generating TypeScript Types from GraphQL Schema in Apollo
TypeScript
49
star
9

easy-mobx-redux-comparison

A comparison of MobX and Redux and React Context API
JavaScript
48
star
10

google-maps-clustering

JavaScript
44
star
11

mobx2020

Demo of modern MobX for 2020
JavaScript
44
star
12

next-route-modal-demo

JavaScript
42
star
13

next-blog

TypeScript
42
star
14

landbnb

Landbnb - An example application implementing a GraphQL API in Rails
Ruby
35
star
15

demo-infinite-scroll

Demo showing useReducer, useContext, IntersectionObserver and XState
JavaScript
32
star
16

react-hook-form-demo

TypeScript
31
star
17

reduce-example

JavaScript
31
star
18

next-prisma-demo

JavaScript
31
star
19

github-stars-graphql

A demo application to show how to use GraphQL in React using Apollo
JavaScript
30
star
20

mapbox-clustering

JavaScript
30
star
21

redux-context-reducers

JavaScript
30
star
22

google-maps-intro

JavaScript
29
star
23

recharts-exodus-area-chart

CSS
27
star
24

use-effect-example

useEffect hook example
JavaScript
27
star
25

demo-google-places-react

JavaScript
25
star
26

msw-example

JavaScript
24
star
27

apollo-nextjs

TypeScript
22
star
28

nextjs-cookies

JavaScript
22
star
29

react-forms-demo

JavaScript
22
star
30

keplergl-demo

JavaScript
21
star
31

prisma-nexus-demo

TypeScript
20
star
32

tag-along

Real-time geo tracking using Rails, ActionCable, React, MobX, and MapBox
Ruby
20
star
33

learning-react-testing-library

Some small demos for me to learn react testing library
JavaScript
18
star
34

next-mongo-demo

JavaScript
18
star
35

react-router-v6-demo

JavaScript
18
star
36

uses.tools

Next.js with GraphQL server
TypeScript
17
star
37

react-cloudinary-example

JavaScript
17
star
38

mobx-decorators-without-ejecting

How to use MobX in create-react-app with decorators without ejecting.
JavaScript
17
star
39

next-auth-demo

JavaScript
16
star
40

the-clima-front

React code for https://www.theclima.com
JavaScript
16
star
41

stripe-nextjs-checkout

TypeScript
16
star
42

generate-og-image

TypeScript
16
star
43

graphql-resume

TypeScript
15
star
44

invoice-mobx-state-tree

MobX State Tree Example
JavaScript
14
star
45

video-state-machine

JavaScript
14
star
46

image-preview-react

TypeScript
14
star
47

graphcms-nextjs

TypeScript
14
star
48

google-maps-threejs

JavaScript
13
star
49

next-google-maps

TypeScript
13
star
50

react-virtualized-demo

JavaScript
12
star
51

mobx2021

TypeScript
12
star
52

edge-shortener

TypeScript
12
star
53

mobx-demo

JavaScript
12
star
54

next-sitemap-demo

TypeScript
12
star
55

secrets-env-vars-nextjs-now

JavaScript
11
star
56

nextjs-feature-flags

TypeScript
11
star
57

node-scraping-demo

JavaScript
11
star
58

mobx-async-actions

An article/demo of MobX async actions
JavaScript
11
star
59

rush-monorepo-test

JavaScript
11
star
60

react-suspense-demo

Demo application showing 4 uses of React Suspense
JavaScript
11
star
61

nextjs-hooks-database

JavaScript
10
star
62

react-query-demo

JavaScript
10
star
63

nextjs-redis-cache

TypeScript
10
star
64

crime-mapping

JavaScript
9
star
65

use-local-storage-state-demo

JavaScript
9
star
66

staggered-framer-motion

JavaScript
9
star
67

managing-state-react

JavaScript
9
star
68

msw-mocked-api

JavaScript
8
star
69

urql-demo

JavaScript
8
star
70

react-stripe-elements-demo

React Stripe Elements demo with Node backend
JavaScript
8
star
71

react-drag-drop-csv-file

CSS
8
star
72

home-demo

Demo React on Rails app
Ruby
7
star
73

slack-app-demo

JavaScript
7
star
74

apollo-pagination-demo

JavaScript
7
star
75

recoil-cart-demo

JavaScript
7
star
76

idioma

Idioma - A ruby engine for managing translations.
Ruby
7
star
77

apollo-codegen-demo

TypeScript
6
star
78

not-render-react

How To NOT Render Something In React
JavaScript
6
star
79

next-react-native-web

JavaScript
6
star
80

husky-demo

TypeScript
5
star
81

remix-upstash-redis

TypeScript
5
star
82

twitter_phoenix

Elixir
5
star
83

use-google-maps-script

React hook for loading Google Maps Script
TypeScript
5
star
84

gqless-demo

gqless - GraphQL Without GQL
TypeScript
5
star
85

immutable-data-react

JavaScript
5
star
86

mock-fetch-jest-demo

JavaScript
5
star
87

create-react-app-mobx

A demo app using create-react-app, mobx, and decorators.
JavaScript
5
star
88

next-zod

JavaScript
4
star
89

next-redirect

CSS
4
star
90

use-pairs

SpeechSynthesis Demo App
JavaScript
4
star
91

recoil-draggable-demo

JavaScript
4
star
92

rate-limit-demo

CSS
4
star
93

nextjs-graphql

TypeScript
4
star
94

maps-demo-hooks

JavaScript
4
star
95

firebase-react-xs-and-os

TypeScript
4
star
96

demo-error-boundary

Demo app showing Error Boundaries in React
JavaScript
3
star
97

leigh-notes-web

JavaScript
3
star
98

demo-mobx-espanol

JavaScript
3
star
99

graphql-git

Leigh Halliday Map, Reduce, Filter, and Pie Charts
JavaScript
3
star
100

padder

Pad the left, pad the right, pad both sides at once.
Ruby
3
star