CerIs
CerIs

Reputation: 567

Importing third party scripts into React

Im trying to use a third party script (mapbox) within the React ecosystem. In traditional javascript I import the script in the then initialise it like so:

<script src='https://api.mapbox.com/mapbox.js/v3.0.1/mapbox.js'></script>

L.mapbox.accessToken = 'pk.xxxtoken';
var map = L.mapbox.map('map', 'mapbox.streets').setView([40, -74.50], 9);

I tried leaving the head of my React app then in componentDidMount() im trying to initialise it but it doesn't know what "L" is:

'L' is not defined    

So I need to import in the script into the component in some way so I had a look at "react-async-script" but Im not sure...

Upvotes: 1

Views: 846

Answers (1)

Aron
Aron

Reputation: 9248

It looks like mapbox has an npm package, so you can install it with npm install --save mapbox and then use it in your react component by importing it

import * as L from 'mapbox';

// do stuff with L here

Upvotes: 2

Related Questions