Search code examples
reactjslifecycleopenlayers-5

React componentDidMount() influence render


I set properties of Panel Component from of App Component state. When i click the button i want to increase (example) map coordinates.It's ok but i must declare of the openlayers variable in componentDidMount() of Panel but componentDidMount once running so coordinates doesn't change the map what can I do ? Please help ... .

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Panel from './Map'


class App extends Component {
  constructor() {
    super();
    this.state = {
      coordinates: [46.41, 40.82]
    }
  }
  render() {

    return (
      <div >
        <Panel coordinates={this.state.coordinates} />
        <div><button onClick={() => {
          this.setState({
            coordinates: [this.state.coordinates[0] + 1, this.state.coordinates[1]]
          })
        }}>Go</button></div>
      </div>
    );
  }
}

export default App;

Panel Component

import React from 'react'
import 'ol/ol.css';
import { Map, View } from 'ol';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
import proj from 'ol/proj/';
import { fromLonLat } from 'ol/proj';

export default class Panel extends React.Component {

    constructor() {
        super();
        this.state = {
            crd: [46, 40]
        }
    }
    GetCoordinates() {
        return this.state.crd
    }

    componentWillReceiveProps(prp) {
        this.setState({ crd: prp.coordinates })
    }
    componentDidMount() {
        const map = new Map({
            target: 'map',
            layers: [
                new TileLayer({
                    source: new OSM()
                })
            ],
            view: new View({
                center: fromLonLat(this.GetCoordinates()) // i need set here,
                zoom: 7
            })
        });
    }
    render() {
        return <div id="map" style={{ width: '700px', height: '500px' }}></div>
    }
}

Solution

  • Open layer has an imperative API, that means that you have to call methods on the map object to update it.

    So first you need to keep a reference of your map when you instanciate it in componentDidMount:

    componentDidMount() {
        this.map = new Map({
            target: 'map',
            layers: [
                new TileLayer({
                    source: new OSM()
                })
            ],
            view: new View({
                center: fromLonLat(this.GetCoordinates()) // i need set here,
                zoom: 7
            })
        });
    }
    

    Then in componentWillReceiveProps you have to "command" the map to update it's center once your state has changed:

    componentWillReceiveProps(prp) {
        this.setState({ crd: prp.coordinates }, function() {
            this.map.getView().setCenter(ol.proj.fromLonLat(this.GetCoordinates()));
        });
    }
    

    Note that here your component does not need to keep the coordinates in its state. You could use a plain instance variable to store the coordinates and avoid the unnecessary render caused by setState:

    ....
    constructor() {
        super();
        this.crd = [46, 40];
    }
    GetCoordinates() {
        return this.crd
    }
    componentWillReceiveProps(prp) {
        this.crd = prp.coordinates;
        this.map.getView().setCenter(
            ol.proj.fromLonLat(this.GetCoordinates())
        );
    }
    ....