简体   繁体   English

反应传单未显示更新状态

[英]react leaflet not showing updated state

I am fetching data from my device storage and want to display the data on a map.我正在从我的设备存储中获取数据并希望在地图上显示数据。 But when I update my state object this.coords.path inside my function showPics() only the testing marker at {lat:1,lng:1} is displayed all the other coordinates are pushed to this.coords.path but not displayed...但是,当我在函数showPics()更新状态对象this.coords.path仅显示{lat:1,lng:1}处的测试标记,所有其他坐标都被推送到this.coords.path但不显示。 ..

I have tried it with the sampleData -array for testing the basic code -> the loop and everything works - just not when updating the state object with new data...我已经尝试使用sampleData -array 来测试基本代码 -> 循环并且一切正常 - 只是在用新数据更新状态对象时没有......

Here is the code:这是代码:

class Map extends React.Component {

    constructor(props) {
      super(props);
      this.coords = {
        path: [
            {lat:1, lng:1}
        ]
      }
    }      
  
    showPics = async() => {

        let {value} = await Storage.get({key: 'path' })
        let arrayPath = JSON.parse(value)

        for( let i=0; i < arrayPath.length; i++) {

            let newArray = {lat: arrayPath[i].latitude, lng: arrayPath[i].longitude}
            this.coords.path.push(newArray)

        }

    }
  
    render() {
  
      const sampleData = [{
        "Id": 1,
        "lat": 54.083336,
        "lng: 12.108811
      },
      {
        "Id": 2,
        "lat": 54.084336,
        "lng": 12.109811
      }]
      
      return (
        <div className="leaflet-container">
          <MapContainer id="map" center={center} zoom={zoom} scrollWheelZoom={false}>          
            <TileLayer
              attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
              url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
            />
  
            { this.coords.path.map(eachData => (
           <Marker
             position= {[eachData.lat, eachData.lng]}
           />
         ))}

          <IonFab vertical="bottom" horizontal="end" slot="fixed">
            <IonFabButton onClick={() => this.showPics()}>
              <IonIcon icon={image}></IonIcon>
            </IonFabButton>
          </IonFab>
        </div>
      )
  
    }
  }

You are trying to set the data to this.coords .您正在尝试将数据设置为this.coords This is not tracked by react and will not be able to update the DOM.这不会被 react 跟踪,也无法更新 DOM。 Instead you can set your data to the component state as below相反,您可以将数据设置为组件状态,如下所示

state = {
   coords: {
    path: [
        {lat:1, lng:1}
    ]
  }
}

Function showPics can be modified as below to set the data to the state using setState which should be picked by the virtualDOM and update accordingly函数showPics可以修改如下以使用setState将数据设置为状态,该状态应由 virtualDOM 选取并相应地更新

showPics = async() => {
    const {coords:{path}} = this.state;
    let {value} = await Storage.get({key: 'path' })
    let arrayPath = JSON.parse(value);
    const paths = [...path];

    for( let i=0; i < arrayPath.length; i++) {
        let newArray = {lat: arrayPath[i].latitude, lng: 
        arrayPath[i].longitude}
        paths.push(newArray)
    }
    this.setState({
       coords: {
         path: [...paths]
       }
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM