why leaflet map doesn't render on react app










-1















I included leaflet map with js implementation instead of jsx implementation but I have a problem as it overflows its scope.



this is how I imported leaflet:



import L from 'leaflet';


and implemented:



componentDidMount() 
// create map
this.map = L.map('map',
center: [49.8419, 24.0315],
zoom: 16,
layers: [
L.tileLayer('http://s.tile.osm.org/z/x/y.png',
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
),
]
);



and usage:



<div id="map" > 


and this is what i got
enter image description here










share|improve this question
























  • Check stackoverflow.com/a/38836996/2626313

    – xmojmr
    Nov 14 '18 at 12:39






  • 2





    Possible duplicate of Leaflet drawing tiles disjointly

    – peeebeee
    Nov 14 '18 at 14:31











  • adding leaflet css file fixed the problem, thanks @xmojmr

    – S.Aminnejad
    Nov 14 '18 at 15:07















-1















I included leaflet map with js implementation instead of jsx implementation but I have a problem as it overflows its scope.



this is how I imported leaflet:



import L from 'leaflet';


and implemented:



componentDidMount() 
// create map
this.map = L.map('map',
center: [49.8419, 24.0315],
zoom: 16,
layers: [
L.tileLayer('http://s.tile.osm.org/z/x/y.png',
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
),
]
);



and usage:



<div id="map" > 


and this is what i got
enter image description here










share|improve this question
























  • Check stackoverflow.com/a/38836996/2626313

    – xmojmr
    Nov 14 '18 at 12:39






  • 2





    Possible duplicate of Leaflet drawing tiles disjointly

    – peeebeee
    Nov 14 '18 at 14:31











  • adding leaflet css file fixed the problem, thanks @xmojmr

    – S.Aminnejad
    Nov 14 '18 at 15:07













-1












-1








-1








I included leaflet map with js implementation instead of jsx implementation but I have a problem as it overflows its scope.



this is how I imported leaflet:



import L from 'leaflet';


and implemented:



componentDidMount() 
// create map
this.map = L.map('map',
center: [49.8419, 24.0315],
zoom: 16,
layers: [
L.tileLayer('http://s.tile.osm.org/z/x/y.png',
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
),
]
);



and usage:



<div id="map" > 


and this is what i got
enter image description here










share|improve this question
















I included leaflet map with js implementation instead of jsx implementation but I have a problem as it overflows its scope.



this is how I imported leaflet:



import L from 'leaflet';


and implemented:



componentDidMount() 
// create map
this.map = L.map('map',
center: [49.8419, 24.0315],
zoom: 16,
layers: [
L.tileLayer('http://s.tile.osm.org/z/x/y.png',
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
),
]
);



and usage:



<div id="map" > 


and this is what i got
enter image description here







javascript reactjs leaflet react-leaflet






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 5:13









Murli Prajapati

1,41411121




1,41411121










asked Nov 14 '18 at 12:12









S.AminnejadS.Aminnejad

84




84












  • Check stackoverflow.com/a/38836996/2626313

    – xmojmr
    Nov 14 '18 at 12:39






  • 2





    Possible duplicate of Leaflet drawing tiles disjointly

    – peeebeee
    Nov 14 '18 at 14:31











  • adding leaflet css file fixed the problem, thanks @xmojmr

    – S.Aminnejad
    Nov 14 '18 at 15:07

















  • Check stackoverflow.com/a/38836996/2626313

    – xmojmr
    Nov 14 '18 at 12:39






  • 2





    Possible duplicate of Leaflet drawing tiles disjointly

    – peeebeee
    Nov 14 '18 at 14:31











  • adding leaflet css file fixed the problem, thanks @xmojmr

    – S.Aminnejad
    Nov 14 '18 at 15:07
















Check stackoverflow.com/a/38836996/2626313

– xmojmr
Nov 14 '18 at 12:39





Check stackoverflow.com/a/38836996/2626313

– xmojmr
Nov 14 '18 at 12:39




2




2





Possible duplicate of Leaflet drawing tiles disjointly

– peeebeee
Nov 14 '18 at 14:31





Possible duplicate of Leaflet drawing tiles disjointly

– peeebeee
Nov 14 '18 at 14:31













adding leaflet css file fixed the problem, thanks @xmojmr

– S.Aminnejad
Nov 14 '18 at 15:07





adding leaflet css file fixed the problem, thanks @xmojmr

– S.Aminnejad
Nov 14 '18 at 15:07












2 Answers
2






active

oldest

votes


















0














Yeah you're missing the css file.



incidentally this is not how you should be using react-leaflet.
componentDidMount is not the place for doing what you're doing. Put that in the render like



import Map, TileLayer from 'react-leaflet'; 
<Map>
<TileLayer url="'http://s.tile.osm.org/z/x/y.png'" />
</Map>





share|improve this answer






























    0














    You have not added the leaflet css file in your index.html.



    For leaflet version 1.3.4, add the following to index.html



    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css"
    integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="
    crossorigin=""/>


    In your map component css file, override the leaflet-container class with desired height and width.



    .leaflet-container 
    height: 600px;
    width: 100%;



    Once you have added that, use Map and TileLayer Components of react-leaflet to render map.



    import React, Component from "react";
    import ReactDOM from "react-dom";
    import Map, TileLayer from "react-leaflet";
    import "./styles.css";

    class App extends Component
    state =
    center: [51.505, -0.091],
    zoom: 13
    ;

    render()
    return (
    <div>
    <Map center=this.state.center zoom=this.state.zoom>
    <TileLayer
    attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
    url="https://s.tile.osm.org/z/x/y.png"
    />
    </Map>
    </div>
    );



    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);


    You can find working code here. https://codesandbox.io/s/2wnv7o1mlr






    share|improve this answer






















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299962%2fwhy-leaflet-map-doesnt-render-on-react-app%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Yeah you're missing the css file.



      incidentally this is not how you should be using react-leaflet.
      componentDidMount is not the place for doing what you're doing. Put that in the render like



      import Map, TileLayer from 'react-leaflet'; 
      <Map>
      <TileLayer url="'http://s.tile.osm.org/z/x/y.png'" />
      </Map>





      share|improve this answer



























        0














        Yeah you're missing the css file.



        incidentally this is not how you should be using react-leaflet.
        componentDidMount is not the place for doing what you're doing. Put that in the render like



        import Map, TileLayer from 'react-leaflet'; 
        <Map>
        <TileLayer url="'http://s.tile.osm.org/z/x/y.png'" />
        </Map>





        share|improve this answer

























          0












          0








          0







          Yeah you're missing the css file.



          incidentally this is not how you should be using react-leaflet.
          componentDidMount is not the place for doing what you're doing. Put that in the render like



          import Map, TileLayer from 'react-leaflet'; 
          <Map>
          <TileLayer url="'http://s.tile.osm.org/z/x/y.png'" />
          </Map>





          share|improve this answer













          Yeah you're missing the css file.



          incidentally this is not how you should be using react-leaflet.
          componentDidMount is not the place for doing what you're doing. Put that in the render like



          import Map, TileLayer from 'react-leaflet'; 
          <Map>
          <TileLayer url="'http://s.tile.osm.org/z/x/y.png'" />
          </Map>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 15:09









          jbccollinsjbccollins

          19713




          19713























              0














              You have not added the leaflet css file in your index.html.



              For leaflet version 1.3.4, add the following to index.html



              <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css"
              integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="
              crossorigin=""/>


              In your map component css file, override the leaflet-container class with desired height and width.



              .leaflet-container 
              height: 600px;
              width: 100%;



              Once you have added that, use Map and TileLayer Components of react-leaflet to render map.



              import React, Component from "react";
              import ReactDOM from "react-dom";
              import Map, TileLayer from "react-leaflet";
              import "./styles.css";

              class App extends Component
              state =
              center: [51.505, -0.091],
              zoom: 13
              ;

              render()
              return (
              <div>
              <Map center=this.state.center zoom=this.state.zoom>
              <TileLayer
              attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
              url="https://s.tile.osm.org/z/x/y.png"
              />
              </Map>
              </div>
              );



              const rootElement = document.getElementById("root");
              ReactDOM.render(<App />, rootElement);


              You can find working code here. https://codesandbox.io/s/2wnv7o1mlr






              share|improve this answer



























                0














                You have not added the leaflet css file in your index.html.



                For leaflet version 1.3.4, add the following to index.html



                <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css"
                integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="
                crossorigin=""/>


                In your map component css file, override the leaflet-container class with desired height and width.



                .leaflet-container 
                height: 600px;
                width: 100%;



                Once you have added that, use Map and TileLayer Components of react-leaflet to render map.



                import React, Component from "react";
                import ReactDOM from "react-dom";
                import Map, TileLayer from "react-leaflet";
                import "./styles.css";

                class App extends Component
                state =
                center: [51.505, -0.091],
                zoom: 13
                ;

                render()
                return (
                <div>
                <Map center=this.state.center zoom=this.state.zoom>
                <TileLayer
                attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                url="https://s.tile.osm.org/z/x/y.png"
                />
                </Map>
                </div>
                );



                const rootElement = document.getElementById("root");
                ReactDOM.render(<App />, rootElement);


                You can find working code here. https://codesandbox.io/s/2wnv7o1mlr






                share|improve this answer

























                  0












                  0








                  0







                  You have not added the leaflet css file in your index.html.



                  For leaflet version 1.3.4, add the following to index.html



                  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css"
                  integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="
                  crossorigin=""/>


                  In your map component css file, override the leaflet-container class with desired height and width.



                  .leaflet-container 
                  height: 600px;
                  width: 100%;



                  Once you have added that, use Map and TileLayer Components of react-leaflet to render map.



                  import React, Component from "react";
                  import ReactDOM from "react-dom";
                  import Map, TileLayer from "react-leaflet";
                  import "./styles.css";

                  class App extends Component
                  state =
                  center: [51.505, -0.091],
                  zoom: 13
                  ;

                  render()
                  return (
                  <div>
                  <Map center=this.state.center zoom=this.state.zoom>
                  <TileLayer
                  attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                  url="https://s.tile.osm.org/z/x/y.png"
                  />
                  </Map>
                  </div>
                  );



                  const rootElement = document.getElementById("root");
                  ReactDOM.render(<App />, rootElement);


                  You can find working code here. https://codesandbox.io/s/2wnv7o1mlr






                  share|improve this answer













                  You have not added the leaflet css file in your index.html.



                  For leaflet version 1.3.4, add the following to index.html



                  <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css"
                  integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="
                  crossorigin=""/>


                  In your map component css file, override the leaflet-container class with desired height and width.



                  .leaflet-container 
                  height: 600px;
                  width: 100%;



                  Once you have added that, use Map and TileLayer Components of react-leaflet to render map.



                  import React, Component from "react";
                  import ReactDOM from "react-dom";
                  import Map, TileLayer from "react-leaflet";
                  import "./styles.css";

                  class App extends Component
                  state =
                  center: [51.505, -0.091],
                  zoom: 13
                  ;

                  render()
                  return (
                  <div>
                  <Map center=this.state.center zoom=this.state.zoom>
                  <TileLayer
                  attribution='&amp;copy <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
                  url="https://s.tile.osm.org/z/x/y.png"
                  />
                  </Map>
                  </div>
                  );



                  const rootElement = document.getElementById("root");
                  ReactDOM.render(<App />, rootElement);


                  You can find working code here. https://codesandbox.io/s/2wnv7o1mlr







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 3:31









                  Murli PrajapatiMurli Prajapati

                  1,41411121




                  1,41411121



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299962%2fwhy-leaflet-map-doesnt-render-on-react-app%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3