[英]React Leaflet first object is undefined in a useState case for drawing map markers
I am trying to loop through an object that has been parsed from a CSV file of Locations, and Lat/Lng pairs.我正在尝试遍历从位置和纬度/经度对的 CSV 文件解析的对象。 But when I loop through this object, the first loop returns my object with all the values as undefined.但是当我遍历这个对象时,第一个循环返回我的对象,所有值都为未定义。 My code is as follows:我的代码如下:
import React, { Component, useState, useEffect } from 'react';
import { LatLng } from 'leaflet';
import { Map, TileLayer, CircleMarker, Popup, Marker } from 'react-leaflet';
import CovidCasesClean from './CovidCasesClean.csv';
import Papa from 'papaparse';
export default function PlaceMarker() {
const latlng = [null];
const [rows, setRows] = React.useState([])
const [data, setData] = React.useState({});
React.useEffect(() => {
async function getData() {
const response = await fetch('/data/CovidCasesClean.csv')
const reader = response.body.getReader()
const result = await reader.read() // raw array
const decoder = new TextDecoder('utf-8')
const csv = decoder.decode(result.value) // the csv text
console.log(csv)
const results = Papa.parse(csv, { header: true, skipEmptyLines: true}) // object with { data, errors, meta }
console.log(results)
const rows = results.data
console.log(rows)
// array of objects
setRows(rows)
rows.map((item, i) => {
const rowObj = {item}
//console.log(rowObj)
console.log(i)
setData({
...data, key: i, ...data, location: rowObj.item.Location, ...data, latitude: parseFloat(rowObj.item.Latitude), ...data, longitude: parseFloat(rowObj.item.Longitude), ...data, cases: parseFloat(rowObj.item.TotalCases)
})
})
}
getData()
}, []) // [] means just do this once, after initial render
return (
<div>{console.log(data.key, data.location, data.latitude, data.longitude, data.cases)}</div>
)
}
When the console line: <div>{console.log(data.key, data.location, data.latitude, data.longitude, data.cases)}</div>
is ran, the first result returns:当控制台行: <div>{console.log(data.key, data.location, data.latitude, data.longitude, data.cases)}</div>
运行时,第一个结果返回:
undefined undefined undefined undefined undefined未定义未定义未定义未定义未定义
whereas the second run through returns the correct object like so:而第二次运行返回正确的对象,如下所示:
0 Barking and Dagenham 53.546299 -1.46786 18 0 巴金和达格南 53.546299 -1.46786 18
This causes my creating a marker object to fail as the first object is not a latlng pair, I have been trying to create markers like so:这导致我创建标记对象失败,因为第一个对象不是 latlng 对,我一直在尝试像这样创建标记:
<Marker position={(data.longitude, data.latitude)}>
<Popup>
<span>
A pretty CSS3 popup.
<br />
Easily customizable.
</span>
</Popup>
</Marker>;
This is where the error occurs ^ Any ideas where I am going wrong, this is my first react project any help appreciated!这是发生错误的地方 ^ 任何我出错的想法,这是我的第一个反应项目,任何帮助表示感谢!
CSV file format: CSV 文件格式:
Location,TotalCases,Latitude,Longitude
Barking and Dagenham,18,53.546299,-1.46786
Barnet,28,51.605499,-0.207715
What you are trying can be achieved in a much simpler way.您可以通过更简单的方式实现您的尝试。
Parse the csv data inside an useEffectHook.解析 useEffectHook 中的 csv 数据。 You do not need any of these:您不需要任何这些:
const response = await fetch('/data/CovidCasesClean.csv') const reader = response.body.getReader() const result = await reader.read() // raw array const decoder = new TextDecoder('utf-8') const csv = decoder.decode(result.value) // the csv text console.log(csv) const response = await fetch('/data/CovidCasesClean.csv') const reader = response.body.getReader() const result = await reader.read() // 原始数组 constdecoder = new TextDecoder('utf-8') const csv =decoder.decode(result.value) // csv 文本 console.log(csv)
const [data, setData] = useState([]);
useEffect(() => {
Papa.parse(csv, {
download: true, // use this option to interpret the input string as a URL from which to download the input file.
header: true,
skipEmptyLines: true,
complete: results => setData(results.data)
});
}, []);
and once you store the csv parsed markers data loop over them to visualize the markers:一旦你存储了 csv 解析的标记数据循环它们以可视化标记:
{data &&
data.map(({ Location, Latitude, Longitude, TotalCases }, i) => (
<Marker
key={`markers-${i}`}
position={[Latitude, Longitude]}
icon={icon}
>
<Popup>
<span>
<b>Location</b>: {Location}
<br />
<b>TotalCases</b>: {TotalCases}
</span>
</Popup>
</Marker>
))}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.