0

I'm very new at programming and react. Currently trying to bring a layer to MapboxGL Map but I become error msg it says:

'mapContainer' is not defined no-undef

What am I doing wrong?

import React, {useEffect} from 'react';
import mapboxgl from 'mapbox-gl';
mapboxgl.accessToken = 'PUBLIC TOKEN'

 const getMap = () => {
return new mapboxgl.Map({
  container: mapContainer,
  style: 'mapbox://styles/mapbox/streets-v11',
  center: [9, 47],
  zoom: 10
});



  const Map = () => {
    useEffect(() => {
      const map = getMap();

  map.on('move', () => {
    this.setState({
      lng: map.getCenter().lng.toFixed(4),
      lat: map.getCenter().lat.toFixed(4),
      zoom: map.getZoom().toFixed(0)
    });
  });

  map.on('load', () => {
    map.addLayer({
      id: 'streets',
      type: 'line',
      source: {
        type: 'geojson',
        data:
          'http://someWFSAPIdata=application/json'
      },
      layout: {
        'line-join': 'round',
        'line-cap': 'round'
      },
      paint: {
        'line-color': '#08363e',
        'line-width': 0.8
      }
    });
  });
}, []);

And rendering as followed

 return (
    <div>
      <div ref={el => (this.mapContainer = el)} className='mapContainer' />
      <h1>Hello there geoReact</h1>
    </div>
  );

Since I'm using functional component no longer need to render but I'm not sure what is wrong with it.

Thanks a lot

2
  • Please use react-mapbox-gl Commented Nov 22, 2019 at 9:55
  • Can you please describe more detailed? please :) @DennisVash? Commented Nov 22, 2019 at 11:05

1 Answer 1

1

I found this explanation which was very helpful for my case

https://sparkgeo.com/blog/build-a-react-mapboxgl-component-with-hooks/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.