Try solve no map changes problem

This commit is contained in:
lukasmartinelli
2016-09-10 00:43:41 +02:00
parent b6dff4aa58
commit c84318e6fe
5 changed files with 52 additions and 22 deletions

View File

@@ -1,6 +1,6 @@
import React from 'react'
import ReactMapboxGl, { ZoomControl } from "react-mapbox-gl"
import MapboxGl from 'mapbox-gl';
import diffStyles from 'mapbox-gl-style-spec/lib/diff'
import theme from './theme.js'
export class Map extends React.Component {
@@ -8,11 +8,33 @@ export class Map extends React.Component {
mapStyle: React.PropTypes.object.isRequired
}
componentWillReceiveProps(nextProps) {
const map = this.state.map
if(map) {
const changes = diffStyles(this.props.mapStyle, nextProps.mapStyle)
changes.forEach(change => {
map[change.command].apply(map, change.args);
});
}
}
shouldComponentUpdate(nextProps, nextState) {
return nextProps.mapStyle !== this.props.mapStyle
}
componentDidMount() {
MapboxGl.accessToken = "pk.eyJ1IjoibW9yZ2Vua2FmZmVlIiwiYSI6IjIzcmN0NlkifQ.0LRTNgCc-envt9d5MzR75w";
const map = new MapboxGl.Map({
container: this.container,
style: this.props.mapStyle,
});
map.on("style.load", (...args) => {
this.setState({ map });
});
}
render() {
return <ReactMapboxGl
style={this.props.mapStyle}
accessToken="pk.eyJ1IjoibW9yZ2Vua2FmZmVlIiwiYSI6IjIzcmN0NlkifQ.0LRTNgCc-envt9d5MzR75w">
<ZoomControl/>
</ReactMapboxGl>
return <div ref={x => this.container = x}></div>
}
}