代码之家  ›  专栏  ›  技术社区  ›  Emin Adiloğlu

React componentDidMount()影响呈现

  •  0
  • Emin Adiloğlu  · 技术社区  · 6 年前

    ? 请帮忙。

    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;
    

    面板组件

    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>
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   remix23    6 年前

    Open layer有一个命令式API,这意味着您必须调用map对象上的方法来更新它。

    所以首先你需要在实例化地图时保留它的引用 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
            })
        });
    }
    

    然后进入 componentWillReceiveProps 一旦你的状态改变,你必须“命令”地图更新它的中心:

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

    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())
        );
    }
    ....