React-ArcGIS
React-ArcGIS is a library of React components which use the ArcGIS API for JavaScript. React-ArcGIS uses esri-loader internally to load and interact with the AMD ArcGIS API for JavaScript, and provide a base for building mapping applications.
Installation:
- Run
npm i react-arcgis
(if you decide you like it, you can even include --save
)
Version 3.1.2:
- Fixed some issues with the distribution. The package is now shipped as both ESM and bundled UMD formats. Please note that I am only officially supporting the latest version of react with this package, but will try my best to keep things backward-compatible. The peer dependency is set to
"react": "*"
.
Version 3.1.0:
- React ArcGIS now uses esri-loader under the hood, as it accomplishes the same thing as esri-promise and is being more actively maintained. React-arcgis provides
loadModules
from esri-loader as esriPromise
so as to not break existing applications.
Changed in version 3.0.0:
- React ArcGIS provides 4 core components -
<Map />
, <Scene />
, <WebMap />
, and <WebScene />
While having a declarative html-like syntax for doing everything in the ArcGIS API for JavaScript would be great, the surface area of the api is very large, and it is regularly updated with new features and functionality. Rather than attempting to wrap the entire thing in a react-like syntax, I have decided to just handle the essentials, and provide clear examples for how to perform more complex tasks by using the api directly (still within your react app of course).
Because you will be less abstracted from Esri's API, you will actually be in a better position to utilize its full functionality!
Basic Usage:
Don't forget to load the js api stylesheet! https://js.arcgis.com/4.5/esri/css/main.css
If you need to support browsers lacking a native promise implementation, you will have to add a global Promise
constructor polyfill to your project, as react-arcgis does not include one. I recommend es6-promise.
Render a simple map in React:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Map } from 'react-arcgis';
ReactDOM.render(
<Map />,
document.getElementById('container')
);
Or, render a 3D web-scene:
import * as ReactDOM from 'react-dom';
import { Scene } from 'react-arcgis';
ReactDOM.render(
<Scene />,
document.getElementById('container')
);
You can also add webmaps and webscenes from ArcGIS Online:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { WebMap, WebScene } from 'react-arcgis';
ReactDOM.render(
<div style={{ width: '100vw', height: '100vh' }}>
<WebMap id="6627e1dd5f594160ac60f9dfc411673f" />
<WebScene id="f8aa0c25485a40a1ada1e4b600522681" />
</div>,
document.getElementById('container')
);
If you want to change the style of the Map
or Scene
, just give it a class:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Scene } from 'react-arcgis';
ReactDOM.render(
<Scene className="full-screen-map" />,
document.getElementById('container')
);
You can also pass properties into the Map
, MapView
, or SceneView
via the viewProperties or mapProperties props:
import * as React from 'react';
import { Map } from 'react-arcgis';
export default (props) => (
<Map
class="full-screen-map"
mapProperties={{ basemap: 'satellite' }}
/>
)
These properties are passed directly to the available properties on the corresponding ArcGIS API classes:
import * as React from 'react';
import { Scene } from 'react-arcgis';
export default (props) => (
<Scene
style={{ width: '100vw', height: '100vh' }}
mapProperties={{ basemap: 'satellite' }}
viewProperties={{
center: [-122.4443, 47.2529],
zoom: 6
}}
/>
)
If you want to access the map
and view
instances directly after they are loaded, pass in an onLoad
handler:
import * as React from 'react';
import { Map } from 'react-arcgis';
export default class MakeAMap extends React.Component {
constructor(props) {
super(props);
this.state = {
map: null,
view: null
};
this.handleMapLoad = this.handleMapLoad.bind(this)
}
render() {
return <Map className="full-screen-map" onLoad={this.handleMapLoad} />;
}
handleMapLoad(map, view) {
this.setState({ map, view });
}
}
Don't forget an onFail
handler in case something goes wrong:
import * as React from 'react';
import { WebScene } from 'react-arcgis';
export default class MakeAScene extends React.Component {
constructor(props) {
super(props);
this.state = {
status: 'loading'
};
this.handleFail = this.handleFail.bind(this);
}
render() {
return <WebScene className="full-screen-map" id="foobar" onFail={this.handleFail} />;
}
handleFail(e) {
console.error(e);
this.setState({ status: 'failed' });
}
}
"Advanced" Usage:
The functionality available through the ArcGIS API for JavaScript goes well beyond just rendering maps, and if your application needs to do more with the map than simply show it, you will quickly find that you need access to the rest of Esri's API.
React-arcgis provides the children of <Map />
, <Scene />
, <WebMap />
, and <WebScene />
with access to their parent's map
and view
instances through props. Combined with esriPromise
, we can use this to easily get other functionality from the ArcGIS JS API and use it within our react application.
For example, let's convert a Bermuda Triangle graphic from this example into a react component:
import * as React from 'react';
import { esriPromise } from 'react-arcgis';
export default class BermudaTriangle extends React.Component {
constructor(props) {
super(props);
this.state = {
graphic: null
};
}
render() {
return null;
}
componentWillMount() {
esriPromise(['esri/Graphic']).then(([ Graphic ]) => {
const polygon = {
type: "polygon",
rings: [
[-64.78, 32.3],
[-66.07, 18.45],
[-80.21, 25.78],
[-64.78, 32.3]
]
};
const fillSymbol = {
type: "simple-fill",
color: [227, 139, 79, 0.8],
outline: {
color: [255, 255, 255],
width: 1
}
};
const graphic = new Graphic({
geometry: polygon,
symbol: fillSymbol
});
this.setState({ graphic });
this.props.view.graphics.add(graphic);
})).catch((err) => console.error(err));
}
componentWillUnmount() {
this.props.view.graphics.remove(this.state.graphic);
}
}
Now we can use the <BermudaTriangle />
component within our <Map />
, <Scene />
, <WebMap />
, or <WebScene />
, and the map
and view
props will automatically be supplied by react-arcgis:
import * as React from 'react';
import { Scene } from 'react-arcgis';
import BermudaTriangle from './BermudaTriangle';
export default (props) => (
<Scene class="full-screen-map">
<BermudaTriangle />
</Scene>
)
Contributions
Anyone is welcome to contribute to this package. My only "rule" is that your contribution must either pass the existing unit tests, or include additional unit tests to cover new functionality.
Here are some commands that may be helpful for development:
npm test
: Runs the unit testsnpm run build
: Builds the application
License
MIT