alex3165 / react-mapbox-gl

A React binding of mapbox-gl-js
http://alex3165.github.io/react-mapbox-gl/
MIT License
1.92k stars 534 forks source link

Uncaught TypeError: Cannot read property '0' of undefined #39

Closed Harrisonl closed 8 years ago

Harrisonl commented 8 years ago

No matter what I do, I can not get the map to actually render. The box itself renders, but is empty and this error mapbox-gl.js?31ef:295 Uncaught TypeError: Cannot read property '0' of undefined appears every time i move my mouse around where the map should be. The cursor changes, so the map box is there, just empty. Below is the component that renders it:

import React from "react"
import cssModules from "react-css-modules"
import style from "./style.css"
import { connect } from 'react-redux'
import ReactMapboxGl, { Layer, Feature } from "react-mapbox-gl"

const config = {
  containerStyle: {height: "100vh", width: "100vw"},
  accessToken: "....",
  style: "mapbox://styles/mapbox/streets-v9"
};

export class Map extends React.Component {
  render() {
    return (
      <div>
        <h1>Hello</h1>
        <ReactMapboxGl
          style={config.style}
          accessToken={config.accessToken}
          containerStyle={config.containerStyle}
        />
      </div>
    )
  }
}

const mapStateToProps = state => {
  return {
    user: state.user
  }
}

export default connect(mapStateToProps)(cssModules(Map, style))
alex3165 commented 8 years ago

Hi @Harrisonl, can you send a link to a jsfiddle or to your project so that i can test it ? i can't reproduce it using the examples.

Harrisonl commented 8 years ago

@alex3165 it's a company project so most likely not sorry. I can probably add in the webpack and package.json file though if that helps? I'll have to add them when I get home.

On a side, is there any other dependencies I need to get this working? I reduced the london example to pretty much the exact same lines of code and replicated the webpack file from the example and it works there. I can't figure out what the difference between the two is.

Harrisonl commented 8 years ago
// package.json
{
  "name": "...",
  "version": "1.0.0",
  "description": "...",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "test": "mocha --require babel-register --require .test-setup.js -R spec app/**/spec.js",
    "cover": "nyc -x '**/*spec.js' -n 'app' -r text -r html -r lcov npm test"
  },
  "author": "Harrison Lucas",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.13.2",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.13.2",
    "babel-preset-react": "^6.11.1",
    "babel-preset-stage-0": "^6.5.0",
    "babel-register": "^6.11.6",
    "css-loader": "^0.23.1",
    "enzyme": "^2.4.1",
    "expect": "^1.20.2",
    "extract-text-webpack-plugin": "^1.0.1",
    "html-webpack-plugin": "^2.22.0",
    "jsdom": "^9.4.2",
    "mocha": "^3.0.2",
    "nyc": "^8.1.0",
    "postcss-cssnext": "^2.7.0",
    "postcss-loader": "^0.10.0",
    "react-addons-test-utils": "^15.3.0",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  },
  "babel": {
    "presets": [
      "es2015",
      "react",
      "stage-0"
    ]
  },
  "dependencies": {
    "halogen": "^0.2.0",
    "history": "^3.0.0",
    "lodash.assign": "^4.2.0",
    "react": "^15.3.0",
    "react-css-modules": "^3.7.10",
    "react-dom": "^15.3.0",
    "react-mapbox-gl": "^0.9.2",
    "react-redux": "^4.4.5",
    "react-router": "^2.6.1",
    "react-router-redux": "^4.0.5",
    "redux": "^3.5.2",
    "redux-thunk": "^2.1.0",
    "whatwg-fetch": "^1.0.0"
  }
}
// webpack
var path = require('path')
var webpack = require('webpack')
var cssnext = require('postcss-cssnext')

module.exports = {
  devtool: 'cheap-module-eval-source-map',
  entry: [
    'webpack-dev-server/client?http://localhost:3000',
    './app/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  plugins: [
    new webpack.DefinePlugin({
      DEV: true
    })
  ],
  module: {
    loaders: [
      // JS Loaders
      {
        test: /\.js$/,
        loaders: ['babel'],
        exclude: /node_modules/,
        include: path.join(__dirname, 'app')
      },
      // CSS Loaders
      {
        test: /\.css$/,
        loader: 'style!css?modules&importLoaders=1&localIdentName=[local]_[hash:base64:5]!postcss',
        include: path.join(__dirname, 'app')
      }
    ]
  },
  resolve: {
    extensions: [ '', '.js' ]
  },
  postcss: function () {
    return [cssnext]
  }
}
lkazberova commented 8 years ago

Hi @Harrisonl ! Try to set pitch={0} in your map's definition. It helps me :)

alex3165 commented 8 years ago

That is surprising because the pitch props already have 0 as a default, but if it solve the issue, it can be a temporary work around.

lkazberova commented 8 years ago

@alex3165 I'm sorry but version (0.9.2) from npm doesn't have default pitch value.

9
alex3165 commented 8 years ago

Ups i will push a new version thanks 👍

alex3165 commented 8 years ago

Should be fixed with the version 0.9.3 :)

Harrisonl commented 8 years ago

oh right. thanks @alex3165 & @lkazberova - I'll have a look when I get home tonight.

Harrisonl commented 8 years ago

Worked perfectly. Thanks guys.