Code Monkey home page Code Monkey logo

react-dynamic-import-esri-js-api's Introduction

This project is example of Create React App dynamically importing/loading ESRI API library.

Live Example

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';

let esriApi = '3.21';

class App extends Component {
  constructor() {
    super();

    this.state = {
      stylePath: null
    }
  }

  loadMap = () => {
    import('./import-modules/EsriLoader')
        .then(({bootstrap, dojoRequire}) => {
          bootstrap((err) => {
            if (err) {
              console.error(err);
            } else {
              this.setState({stylePath: `https://js.arcgis.com/${esriApi}/esri/css/esri.css`});
              dojoRequire(
                  [
                    'esri/map'
                  ],
                  (Map) => {
                    let map = new Map('mapMount', {
                      center: [-100, 30],
                      zoom: 4,
                      basemap: 'gray'
                    });
                    window.map = map;

                  });
            }
          }, {
            url: `https://js.arcgis.com/${esriApi}/`
          });
        })
        .catch(err => {
          // Handle failure
        });
  };

  render() {
    return (
      <div className="App">
        <div className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h2>React JS – Dynamically Importing ESRI JS API</h2>
        </div>
        <p className="App-intro">
          To get started, edit <code>src/App.js</code> and save to reload.
        </p>
        <button onClick={this.loadMap}>Load Map</button>
        <div id="mapMount"/>
        <link rel="stylesheet" type="text/css" href={this.state.stylePath} />
      </div>
    );
  }
}

export default App;

alt text

react-dynamic-import-esri-js-api's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.