React-mapbox-gl: ES5 require not working

Created on 13 Mar 2017  路  4Comments  路  Source: alex3165/react-mapbox-gl

Hi there, I鈥檓 conditionally requiring ReactMapboxGL on the client side only so I can鈥檛 use import but when I use

var ReactMapboxGl = require("react-mapbox-gl").default // Note the default was necessary too
var Layer = ReactMapboxGl.Layer
var Feature = ReactMapboxGl.Feature

I get the following error

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in.

The map loads ok, but no Layers, Markers or Features show.

I had a little google and it seems like the problem might be to do with transpiled Typescript?

This is the relevant code in my repo

Thanks in advance for any help

Bug Help wanted

All 4 comments

Thanks for the issue, I will look into it when i have some free time

This is because you're accessing module.default.Layer instead of module.Layer ;)

@philpl if I use module.layer then the page fails to load entirely, I get the error

Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

I got it working in the end using

var ReactMapboxGl = require('react-mapbox-gl')
var ReactMapboxGlComponent = ReactMapboxGl.default
var Layer = ReactMapboxGl.Layer
var Feature = ReactMapboxGl.Feature
var Marker = ReactMapboxGl.Marker

followed by

<ReactMapboxGlComponent
  ...>
  <Layer
    ...>
    <Feature coordinates={routeLine}/>
  </Layer>
</ReactMapboxGlComponent>
Was this page helpful?
0 / 5 - 0 ratings

Related issues

edv4rd0 picture edv4rd0  路  3Comments

kn0ll picture kn0ll  路  3Comments

spybaby1 picture spybaby1  路  4Comments

DaveBathnes picture DaveBathnes  路  3Comments

13milliseconds picture 13milliseconds  路  4Comments