我Mapbox
用来尝试从本地 JSON 文件呈现各种坐标。
但是,我收到错误消息TypeError: Object(...) is not a function
。我查找了一些可能导致它的信息(例如,早期版本的react
不会与钩子一起使用),但由于它非常模糊,我不知道如何正确诊断它。
我有一个使用 React 钩子的组件,我相信它是导致错误的原因。我无法判断我的某个软件包是否已过时,是否与函数的编写/设置/等方式有关,或者是否是其他原因。
这是一个显示完整组件代码的 JS Fiddle:https ://jsfiddle.net/bsp7q839/
包.json:
"dependencies": {
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-map-gl": "^5.2.3",
"react-mapbox-gl": "^4.8.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"reactstrap": "^8.4.1",
"sass": "^1.26.1"
},
组件.js:
import React, { useState, useEffect } from 'react';
import ReactMapGL, { Marker, Popup } from "react-map-gl";
import * as crimeData from "../data/casefile-json.json";
export default function CrimeMap() {
const [viewport, setViewport] = useState({
// latitude, longitude, etc
})
const [selectedLocation, setSelectedLocation] = useState(null);
useEffect(() => {
const listener = (e) => {
if (e.key === 'Escape') {
setSelectedLocation(null);
}
};
window.addEventListener("keydown", listener);
return () => {
window.removeEventListener("keydown", listener)
}
}, [])
// etc
应用程序.js:
import React from 'react';
import { BrowserRouter, Route, Switch } from "react-router-dom";
import Header from "./components/Header.js";
import CrimeMap from "./components/CrimeMap.js";
import './App.css';
function App() {
return (
<BrowserRouter>
<div className="App">
<Header />
<CrimeMap />
<Switch>
<Route path="/crime_map" component={CrimeMap} exact />
</Switch>
{/* <Footer /> */}
</div>
</BrowserRouter>
)
}
export default App;
JSON 片段:
{
//
"fields": [
{
"name": "FID",
"type": "esriFieldTypeOID"
}
],
"cases": [
{
"attributes": {
"CaseNumber": 1,
"CaseName": "The Wanda Beach Murders",
"Episode Date (M/D/Y)": "1/9/2016",
"Length of Case": "1:14:02",
"Researched by": "Anonymous Host",
"CountryOfCrime": "Australia",
"StateOfCrime": "New South Wales",
"CityOfCrime": "Sydney"
},
"geometry": {
"x": -33.868820,
"y": 151.209290
}
},