我有一个ARC GIS反应组件,它扩展了一个基本的“父”组件:

import React, { Component } from "react"
import "./AOPMap.css"
import { Map } from 'react-arcgis';
import BoundaryLayers from '../BoundaryLayers/BoundaryLayers'
import AOPComponent from '../AOPComponent/AOPComponent'

class AOPMap extends AOPComponent {
  constructor(props) {
    super(props);

    this.handleMapLoad = this.handleMapLoad.bind(this)
  }

  render() {
      alert ('Reached render');
      return (
        <Map onLoad={this.handleMapLoad} >
          <BoundaryLayers boundaryType={this.state.boundaryType} map={this.state.map} />
        </Map>
      );
  }

  handleMapLoad(map, view) {
      this.setState({ map, view });
  }

}

export default AOPMap;

此表还包括扩展此基本父组件:

import React, { Component } from "react"
import "./AOPForm.css"
import { Map } from 'react-arcgis';
import BoundaryLayers from '../BoundaryLayers/BoundaryLayers'
import AOPComponent from '../AOPComponent/AOPComponent'

class AOPForm extends AOPComponent {
    constructor(props) {
        super(props);

        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      }

      handleChange(event) {
        console.log("Event Value: " + event.target.value);
        this.setState({boundaryType: event.target.value});
        alert('Boundary Type submitted as: ' + this.state.boundaryType);
      }

      handleSubmit(event) {
        // NOTE: Now - it will typically report back as what you changed it to - doesn't seem to pick it up on Change right away?
        alert('Boundary Type submitted as: ' + this.state.boundaryType);
        event.preventDefault();
      }

      render() {
          alert ('Reached render');
        return (
          <form onSubmit={this.handleSubmit}>
            <label>
              Pick a Boundary Type:
              <select value={this.state.boundaryType} onChange={this.handleChange}>
                <option value="point">Point</option>
                <option value="tile">Tile</option>
                <option value="flightline">Flightline</option>
              </select>
            </label>
            <input type="submit" value="Submit" />
          </form>
        );
      }
}

export default AOPForm;

因此,在现在更改下拉列表时,我可以通过一些基本警报看到状态不会保证它会立即显示出来 . 但是,真正的问题是,当我更改状态时,我看到它重新呈现表单(由于警报"Reached render") - 但它似乎没有刷新Map也使用this.state.boundaryType .

正在修改的状态在父组件中定义,在构造函数中为此定义:

import React, { Component } from "react"
import "./AOPComponent.css"
import { Map } from 'react-arcgis';
import BoundaryLayers from '../BoundaryLayers/BoundaryLayers'

class AOPComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
        map: {basemap: 'satellite'},
        view: null,
        loadedKmlLayers: [],
        boundaryType: 'point'
    };

    this.handleComponentLoad = this.handleMapLoad.bind(this)
  }

  handleMapLoad(map, view) {
      this.setState({ map, view });
  }

}

export default AOPComponent;

所以我的想法是,如果状态发生变化,它会重新渲染,为什么当我们改变父级的状态时,只有Form重新渲染而不是Map?当用户准备好提交表单时,触发重新渲染Map的正确方法是什么(将有多个字段 - 不仅仅是这一个 - 所以重新渲染Map on Change可能有点多 - 理想情况下,只想在提交时触发重新渲染Map,因此我将其作为占位符进行处理 .

最后,尽管代码可能并不重要,但我们使用“Layout.js”渲染它们,它们只返回div等等 . 包含像Map这样的东西等等 .