<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmed Rafi Ullah</title>
    <description>The latest articles on DEV Community by Ahmed Rafi Ullah (@ahmedrafiullahk).</description>
    <link>https://dev.to/ahmedrafiullahk</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F89097%2F9f6582aa-0624-468a-a703-92009f632e0e.png</url>
      <title>DEV Community: Ahmed Rafi Ullah</title>
      <link>https://dev.to/ahmedrafiullahk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmedrafiullahk"/>
    <language>en</language>
    <item>
      <title>How to integrate Mapbox GL in react using hooks</title>
      <dc:creator>Ahmed Rafi Ullah</dc:creator>
      <pubDate>Mon, 10 Aug 2020 20:47:54 +0000</pubDate>
      <link>https://dev.to/ahmedrafiullahk/how-to-integrate-mapbox-gl-in-react-using-hooks-3a44</link>
      <guid>https://dev.to/ahmedrafiullahk/how-to-integrate-mapbox-gl-in-react-using-hooks-3a44</guid>
      <description>&lt;p&gt;Hi everybody this is my first post of hopefully many (knocks on wood). In this post i'll show how you can get mapbox gl to work in your react app. So lets get started.&lt;/p&gt;

&lt;p&gt;This is what we will build&lt;/p&gt;

&lt;p&gt;Below is a live working copy of the app we just built in code sandbox. You need to replace the access token with your own&lt;br&gt;
&lt;iframe src="https://codesandbox.io/embed/mapbox-gl-react-hooks-f3v12"&gt;
&lt;/iframe&gt;
  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you want to just use class components then mapbox has you covered. They have their own tutorial for that at &lt;a href="https://docs.mapbox.com/help/tutorials/use-mapbox-gl-js-with-react/"&gt;https://docs.mapbox.com/help/tutorials/use-mapbox-gl-js-with-react/&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The web app in the cover image is live at &lt;a href="https://covid-19-dashboard-react.now.sh/"&gt;https://covid-19-dashboard-react.now.sh/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is what i assumed youve already have done by now&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Setup your react app using creat-react-app or using any other 
method.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ok first thing you need to add is the mapbox-gl dependency&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i mapbox-gl
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;next in your html file inside the public folder (assuming cra was used) add the following link&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link
      href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css"
      rel="stylesheet"
    /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we set the access token (here youll need to use your own tokens)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mapboxgl.accessToken =
  "pk.eyJ1Ijoia2hhdHRha2FobWVkIiwiYSI6ImNrZG95em15bDBwb3MyeHR2YWVvdGkwbnEifQ.Ac_1yEJqfx1X8aw1y1yiag";

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next in your App.js or anywhere really create the component which will contain mapbox&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {

  return (
    &amp;lt;div className="App"&amp;gt;

    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then use useState to store the lat,long and zoom level of the map. And useRefs to store references to the map object and the map html element in one place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  const [state, setState] = useState({
    lng: 5,
    lat: 34,
    zoom: 2
  });

  const mapContainer = useRef("");
  const map = useRef(null);


  return (
    &amp;lt;div className="App"&amp;gt;

    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we setup useEffect to run the map initialization code on component mount&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  const [state, setState] = useState({
    lng: 5,
    lat: 34,
    zoom: 2
  });

  const mapContainer = useRef("");
  const map = useRef(null);

  useEffect(() =&amp;gt; {
   // note how i set the map ref over here
    map.current = new mapboxgl.Map({
   // i also use the mapContainer ref to set the mapbox container option
      container: mapContainer.current,
      style: "mapbox://styles/mapbox/streets-v11",
      center: [state.lng, state.lat],
      zoom: state.zoom
    });
  }, []);

  return (
    &amp;lt;div className="App"&amp;gt;

    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we setup the mapContainer ref&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  const [state, setState] = useState({
    lng: 5,
    lat: 34,
    zoom: 2
  });

  const mapContainer = useRef("");
  const map = useRef(null);

  useEffect(() =&amp;gt; {
    map.current = new mapboxgl.Map({
      container: mapContainer.current,
      style: "mapbox://styles/mapbox/streets-v11",
      center: [state.lng, state.lat],
      zoom: state.zoom
    });
  }, []);

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;div style={style} ref={(el) =&amp;gt; (mapContainer.current = el)} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As of now the code should work but it does not show because we havent added styling to map yet&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const style = {
  position: "absolute",
  top: 0,
  right: 0,
  left: 0,
  bottom: 0
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And voila you should have your map running&lt;/p&gt;

&lt;p&gt;Thanks for reading !&lt;/p&gt;

</description>
      <category>mapbox</category>
      <category>react</category>
      <category>reacthooks</category>
      <category>integration</category>
    </item>
  </channel>
</rss>
