npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

esri-loader-hooks

v0.4.0

Published

Custom React hooks for using the ArcGIS API for JavaScript with esri-loader

Downloads

1,239

Readme

esri-loader-hooks

Custom React hooks for using the ArcGIS API for JavaScript with esri-loader.

Install

npm install --save esri-loader esri-loader-hooks or yarn add esri-loader esri-loader-hooks

Usage

import {
  useMap, useScene, useWebMap, useWebScene, // create a map or scene
  useEvent, useEvents, useWatch, useWatches, // handle events or property changes
  useGraphic, useGraphics // add graphics to a map/scene
} from 'esri-loader-hooks';

This library provides a handful of hooks for loading ArcGIS maps and scenes in you components, and then registering event or watch handles, or adding graphics.

Configure esri-loader

Before using these hooks you'll need to at least load the ArcGIS API styles and optionally configure esri-loader.

Maps, Scenes, and Views

You'll want to start with one of the hooks for working with maps, scenes, and their associated views.

All of these hooks return an array where the first element is a ref you use to set the DOM node to be used as the view's container, and the second element is the instance of the view, which you can use with the handler and graphics hooks below, or in your own hooks.

See below for more information on working with view hooks.

useWebMap

Load a WebMap in a MapView.

import React from 'react';
import { useWebMap } from 'esri-loader-hooks';

function WebMap() {
  // takes map as a string (item id), or JSON (item data)
  const [ref] = useWebMap('e691172598f04ea8881cd2a4adaa45ba');
  return <div style={{ height: 400 }} ref={ref} />;
}

You can also override view properties by passing options.

useWebScene

Load a WebScene in a SceneView.

import React from 'react';
import { useWebScene } from 'esri-loader-hooks';

function WebScene() {
  // takes scene as a string (item id), or JSON (item data)
  const [ref] = useWebScene('3a9976baef9240ab8645ee25c7e9c096');
  return <div style={{ height: 400 }} ref={ref} />;
}

You can also override view properties by passing options.

useMap

Load a Map in a MapView with the given options.

import React from 'react';
import { useMap } from 'esri-loader-hooks';

function MapView() {
  // takes initial map and view properties as POJOs
  const map = {
    basemap: "streets"
  };
  const options = {
    view: {
      center: [15, 65],
      zoom: 4
    }
  };
  const [ref] = useMap(map, options);
  return <div style={{ height: 400 }} ref={ref} />;
}

useScene

Load a Map in a SceneView with the given options.

import React from 'react';
import { useScene } from 'esri-loader-hooks';

function SceneView() {
  // takes initial map and view properties as POJOs
  const map = {
    basemap: "streets",
    ground: "world-elevation"
  };
  const options = {
    view: {
      scale: 50000000,
      center: [-101.17, 21.78]
    }
  };
  const [ref] = useScene(map, options);
  return <div style={{ height: 400 }} ref={ref} />;
}

FeatureTables

You can use the useFeatureTable hook to load a stand-alone table or use it in conjunction with a view containing the layer that the table is configured with to enable table to map or table to scene interaction.

Like the hooks above, this hook returns an array where the first element is a ref you use to set the DOM node to be used as the table's container, and the second element is the instance of the table, which you can use with the handler hook below, or in your own hooks.

useFeatureTable

The useFeatureTable hook takes a layer parameter which can be a layer item id, a url to a feature layer or scene layer, or a feature or scene layer instance. The second optional parameter is an options object used to set any constructor options on the FeatureTable. See options for general information about this parameter or FeatureTable properties for the specific table options you can set.

Load a stand-alone FeatureTable from a layer's URL without a selector column:

import React from 'react';
import {useFeatureTable} from 'esri-loader-hooks';

function FeatureTable() {
  const layerUrl = 'https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/CollegesUniversities/FeatureServer/0';
  const tableOptions = {
    visibleElements: { selectionColumn: false }
  };
  const [ref] = useFeatureTable(layerUrl, tableOptions);
  return <div style={{ height: 600 }} ref={ref} />;
}

Events and watches

Once you've used one of the above hooks to load a view, you can register event handlers or watch for property changes with the hooks below.

All of these hooks clean up after themselves by removing the event or watch handle when the callback changes or the component will un-mount.

useEvent

import React from 'react';
import { useMap, useEvent } from 'esri-loader-hooks';

function ClickableMap({ onClick }) {
  const map = {
    basemap: "streets"
  };
  const options = {
    view: {
      center: [15, 65],
      zoom: 4
    }
  };
  const [ref, view] = useMap(map, options);
  // we use the second element returned above to get the view instance
  useEvent(view, "click", onClick);
  return <div style={{ height: 400 }} ref={ref} />;
}

useEvents

You can register the same callback for multiple events with useEvents(view, arrayOfEventNames, callback).

useWatch

You can watch for changes to the view, or any instance of Accessor with the hooks below.

import React from 'react';
import { useScene, useWatch } from 'esri-loader-hooks';

function WatchedScene({ onUpdateChange }) {
  const map = {
    basemap: "streets",
    ground: "world-elevation"
  };
  const options = {
    view: {
      scale: 50000000,
      center: [-101.17, 21.78]
    }
  };
  const [ref, view] = useScene(map, options);
  // we use the second element returned above to get the view instance
  useWatch(view, 'updating', onUpdateChange);
  return <div style={{ height: 400 }} ref={ref} />;
}

useWatches

You can use the same callback to watch changes to multiple properties with useWatches(anyAccessor, arrayOfPropertyNames, callback).

Graphics

Sometimes you have a component that takes a property like coordinates, or an array of geocoding results, and needs to show them as graphics on the view. The hooks below let you do that.

useGraphic

import React from "react";
import { useMap, useGraphics } from "esri-loader-hooks";

// hooks allow us to create a map component as a function
function PointMap({ latitude, longitude }) {
  const geometry = {
    type: "point", // autocasts as new Point()
    latitude,
    longitude
  };
  var symbol = {
    type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
    color: [226, 119, 40],
  };
  const map = {
    basemap: "hybrid"
  };
  const options = {
    view: {
      center: [longitude, latitude],
      zoom: 3
    }
  };
  const [ref, view] = useMap(map, options);
  // takes a view instance and graphic as a POJO
  // the point will be replaced if the lat/lng props change
  useGraphic(view, { geometry, symbol });
  return <div style={{ height: 400 }} ref={ref} />;
}

useGraphics

You can add multiple graphics at the same time with useGraphics(view, arrayOfJsonGraphics).

Working with view hooks

Arguments

All of these hooks take an optional options hash as the final argument. You can pass initial view properties via options.view.

One optional property you can pass to the options hash that is not a view or map constructor option is portalUrl. Most users will not need to set this and it defaults to ArcGIS Online ('https://www.arcgis.com'). However, if you have a private portal or are a member of the Early Adopters program you may need to load an item or layer from somewhere other than the default ArcGIS Online portal.

NOTE: All of the arguments to these hooks will only be used in the map and view constructors. Even if you pass in component props or state the corresponding instance properties will not be updated as your component updates.

If you need to "bind" map and/or view properties to props or state, you can add your own useEffect() hook like this:

import React, { useEffect } from 'react';
import { useMap } from 'esri-loader-hooks';

function ZoomingMap({ zoom = 4 }) {
  const map = {
    basemap: 'streets'
  };
  const options = {
    view: {
      center: [15, 65],
      // pass zoom in options to set initial zoom (in constructor)
      zoom
    }
  };
  const [ref, view] = useMap(map, options);
  // watch for changes to zoom prop and update view
  useEffect(() => {
    if (!view) {
      // view hasn't been created yet
      return;
    }
    if (view.zoom !== Math.round(zoom, 0)) {
      // zoom prop has changed, update view
      view.zoom = zoom;
    }
  }, [zoom, view]);
  return <div style={{ height: 400 }} ref={ref} />;
}

Clean up

All of these hooks clean up after themselves by destroying the view instance when the component will un-mount.

FAQ

Do these hooks work with version 3.x of the ArcGIS API?

No.

How does this compare to react-arcgis?

This library is like a hooks version of react-arcgis, which is a library of a few generic components to get you started using esri-loader. My hypothesis is that a library of generic hooks will be more useful than generic components. This is because the hooks should be easier to compose into many different custom components.

Can I use this with the @arcgis/webpack-plugin?

No. The view and graphics hooks use esri-loader.

That said, you probably don't need a library like this if you're using the webpack plugin. The view and graphics hooks help deal with the complexity introduced by the fact that loadModules() is always asynchronous.

The event and watch hooks could be used without esri-loader. For now you can copy and paste them into your application. If we add more hooks there, it may make sense to move those into their own library that could be used in both types of applications.

Can I use this in my production app?

I'm not (yet), mainly because I don't have a production React app, but you go ahead.

Contributing

See CONTRIBUTING.md.

Development

See the development instructions.