Ad
Cannot Read Property 'Tile' Of Undefined In ReactJS
I am adding a map using Open Layer in React JS and I have imported all the required libraries. but I am getting an error as "Cannot read property 'Tile' of undefined".
What is wrong in the below code?
Code:
import React, { Component } from 'react';
import 'ol/ol.css';
import ol from 'ol';
import Map from 'ol/map';
import View from 'ol/view';
import Tile from 'ol/layer/tile';
class MapComponent extends Component {
componentDidMount() {
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([37.41, 8.82]),
zoom: 4
})
})
}
render() {
return (
<div id="map"></div>
)
}
}
export default MapComponent;
Ad
Answer
I would recommend to use some reactified libs, like react-ol
for instance. Although for this particular question try just Tile
instead of ol.layer.Tile
and apply same principle for consequetive errors of same type. The following code worked for me: here is the codebox
import React, { Component } from "react";
import ReactDOM from "react-dom";
import 'ol/ol.css';
import ol from 'ol';
import Map from 'ol/map';
import View from 'ol/view';
import Tile from 'ol/layer/tile';
import OSM from 'ol/source/osm';
import proj from 'ol/proj';
class MapComponent extends Component {
componentDidMount() {
var map = new Map({
target: 'map',
layers: [
new Tile({
source: new OSM()
})
],
view: new View({
center: proj.fromLonLat([37.41, 8.82]),
zoom: 4
})
})
}
render() {
return (
<div id="map"></div>
)
}
}
Ad
source: stackoverflow.com
Related Questions
- → Import statement and Babel
- → should I choose reactjs+f7 or f7+vue.js?
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → .tsx webpack compile fails: Unexpected token <
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → React Native with visual studio 2015 IDE
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
- → How do I determine if a new ReactJS session and/or Browser session has started?
- → Alt @decorators in React-Native
- → How to dynamically add class to parent div of focused input field?
Ad