【问题标题】:useEffect "is called in function "onEachCountry" that is neither a React function component nor a custom React Hook functionuseEffect "在函数 "onEachCountry" 中调用,该函数既不是 React 函数组件也不是自定义 React Hook 函数
【发布时间】:2021-03-23 18:05:09
【问题描述】:

我正在尝试使用 geoJSON 和以下 api 将冠状病毒病例按国家/地区放在平面球地图上的弹出行 42 中: https://corona.lmao.ninja/v3/covid-19/countries 但是,这告诉我错误“第 23:3 行:React Hook “useEffect”在函数“onEachCountry”中被调用,该函数既不是 React 函数组件也不是自定义 React Hook 函数。React 组件名称必须以大写字母 react-hook 开头“我用 axios 调用我的 api 并使用 latest 来索引数据

你有解决办法吗?

这是我的代码

import React, { useEffect, useState} from 'react';
import axios from "axios";
import { Map, GeoJSON } from "react-leaflet";
import "leaflet/dist/leaflet.css";
import "./CovidMap.css";



const CovidMap = ({ countries }) => {
    const mapStyle = {
        fillColor: "white",
        weight: 1,
        color: "black",
        fillOpacity: 1,
    };

    const onEachCountry = (country, layer)=>{

 const [latest, setLatest] = useState([]);
 

  // connexion api 
  useEffect(() => {
    axios
      .all([
           //donnees par pays 
        axios.get("https://corona.lmao.ninja/v3/covid-19/countries"),
     
      ])
      .then(responseArr => {
        setLatest(responseArr[0].data);
      })
      .catch(err => {
        console.log(err);
      });
  }, []);


        layer.options.fillColor = country.properties.color;
        const name = country.properties.ADMIN; //parametrage popup pays 
        const cases = country.properties.cases;
            layer.bindPopup(`${name} ${latest.cases}`);
    }

    console.log(countries);
    return (
        <Map style={{ height: "90vh" }} zoom={2} center={[20, 100]}>
            <GeoJSON
                style={mapStyle}
                data={countries}
                onEachFeature={onEachCountry} />
        </Map>
    );
};

export default CovidMap;

【问题讨论】:

    标签: json reactjs api


    【解决方案1】:

    您在onEachCountry 中调用useEffect,正如您收到的错误所见,这是不允许的。有关更多信息,请参阅 React 文档。 https://reactjs.org/docs/hooks-effect.html.

    我认为您正在尝试的是以下内容:

    import React, { useEffect, useState} from 'react';
    import axios from "axios";
    import { Map, GeoJSON } from "react-leaflet";
    import "leaflet/dist/leaflet.css";
    import "./CovidMap.css";
    
    
    
    const CovidMap = ({ countries }) => {
        const mapStyle = {
            fillColor: "white",
            weight: 1,
            color: "black",
            fillOpacity: 1,
        };
    
        const [latest, setLatest] = useState([]);
    
        // connexion API 
        useEffect(() => {
          axios
            .all([
               //donnees par pays 
              axios.get("https://corona.lmao.ninja/v3/covid-19/countries"),
         
              ])
              .then(responseArr => {
                setLatest(responseArr[0].data);
              })
              .catch(err => {
                console.log(err);
              });
         }, []);
    
       const onEachCountry = (country, layer)=> {
            layer.options.fillColor = country.properties.color;
            const name = country.properties.ADMIN; //parametrage popup pays 
            const cases = country.properties.cases;
            layer.bindPopup(`${name} ${latest.cases}`);
        }
    
        return (
            <Map style={{ height: "90vh" }} zoom={2} center={[20, 100]}>
                <GeoJSON
                    style={mapStyle}
                    data={countries}
                    onEachFeature={onEachCountry} />
            </Map>
        );
    };
    
    export default CovidMap;
    

    【讨论】:

    • ty 但在第 38 行,编辑器仍然告诉我案件编号未定义:在我的地图和我的卡片中显示选择的国家名称后未定义,而不是案件数量
    • @MendosaHenry 在对上述端点的 get 请求之后,cases 似乎是 countryInfo 对象中的一个键。我不确定您指的是哪一行,但我假设您想要 country.countryInfo.cases 而不是 country.properties.cases
    猜你喜欢
    • 1970-01-01
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 2022-10-02
    • 2020-07-04
    • 2021-11-27
    • 1970-01-01
    • 2023-02-19
    相关资源
    最近更新 更多