Categories
Development

React and hooks – Getting TypeError: Object(…) is not a function

I’m using Mapbox to try to render various coordinates from a local JSON file.

However, I’m receiving the error TypeError: Object(...) is not a function. I’ve looked up some info on what could be causing it (e.g. earlier versions of react won’t work with hooks), but since it’s pretty vague I don’t know how to diagnose it properly.

I have a component that’s using React hooks and I believe it’s responsible for the error. I can’t tell if one of my packages is out of date, if it’s with the way the function is written/set up/etc, or if it’s something else.

Here’s a JS Fiddle showing the complete Component code: https://jsfiddle.net/bsp7q839/


package.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"
  },

Component.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

App.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 snippet:

{
  //
  "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
        }
      },

Leave a Reply

Your email address will not be published. Required fields are marked *