esri-loader-react
esri-loader-react is a small React component that wraps esri-loader.
esri-loader and esri-loader-react
If you’re doing web development with the Esri JS API then chances are that you’ve encountered issues with the dojo loader either using custom libraries, different JS frameworks or modern build tooling. esri-loader is a great way to circumvent these issues. It works by injecting the Esri JS API into your page at runtime so you only use the dojo loader for any Esri JS API modules that you need. It was originally written by @tomwayson and he has a great blog post on some of the challenges for this type of approach.
The API for esri-loader exposes 3 functions
isLoaded()
bootstrap(callback: Function, options = {} as any)
dojoRequire(modules: string[], callback: Function)
The typical workflow then becomes:
- check if the Esri JS API is loaded using
isLoaded
- if not then
bootstrap
the API, this injects the script - if already loaded or when the script is ready then use
dojoRequire
to load Esri modules
Note that you still need to manage loading the CSS for the Esri JS API yourself, so adding it to the
head
section of yourhtml
is still required
esri-loader works with a variety of libraries and frameworks but if you are using React then you can use esri-loader-react. This is a component that essentially wraps some of the logic you would typically use with esri-loader so that you have less to do. The part it wraps is the initial call to isLoaded
and bootstrap
so that once it is ready then you can use dojoRequire
as you would normally with esri-loader. It does this by using the React lifecycle event componentDidMount
. So in your application you can mount esri-loader-react and think of it as the script tag for the Esri JS API. You only need to do this in one place in your application (the top most component for the application or route) and then you use dojoRequire
from one or more other places in your application.
The API for esri-loader-react exposes 2 properties
options
- this is passed through to thebootstrap
function in esri-loaderready
- a function to be called when the component has mounted and the Esri JS API is ready
Usage
As well as previous advantages of using these libraries you can also benefit from pre or lazy loading the Esri JS API in your application rather than on initial page load. In its simplest usage you can do
|
|
comparing this to using esri-loader which would be
|
|
both work the same so feel free to use whichever you prefer. You could also convert it to a functional component.
|
|
which you could then call from somewhere in your app
|
|
Examples
Some examples that use esri-loader-react are
- create-react-app-esri-loader
- esri-react-router-example
- arcgis-react-redux-legend
- preact-legend yes it works with Preact too
I haven’t used this but it looks to do something similar to esri-loader so check it out esri-promise