【问题标题】:I can't figure out how to add Typescript to my React useState hooks, what am I doing wrong?我不知道如何将 Typescript 添加到我的 React useState 钩子中,我做错了什么?
【发布时间】:2021-09-27 16:23:26
【问题描述】:

我在将 Typescript 添加到我的 React 代码中时遇到了很多问题,特别是使用 useSate 钩子。

过去几天我一直在研究如何解决这个问题,但我不确定需要将什么传递给钩子。我应该创建一个单独的.ts 文件来存储我期望在挂钩中的内容吗?当我滚动到带下划线的错误时,它会不断显示如下错误:

“Map”类型的参数不能分配给“SetStateAction”类型的参数。 类型 'Map' 与签名 '(prevState: undefined): undefined' 不匹配。

或

类型 '{ children?: ReactNode; 上不存在属性 'features' }'.`

我已包含我的代码以提供更好的视角,并从我正在使用的源代码中保留在 cmets 中。

import { FC } from "react";
import { useState, useEffect, useRef } from "react";
import "./mapwrapper.css";

// openlayers
import Map from "ol/Map";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import XYZ from "ol/source/XYZ"; //here...

import { fromLonLat } from "ol/proj";

const Home: FC<{}> = (props) => {
  // set intial state
  const [map, setMap] = useState();
  const [featuresLayer, setFeaturesLayer] = useState();

  // pull refs
  const mapElement = useRef<HTMLDivElement>(null);

  // create state ref that can be accessed in OpenLayers onclick callback function
  //  https://stackoverflow.com/a/60643670
  const mapRef = useRef<{}>();
  mapRef.current = map;

  // initialize map on first render - logic formerly put into componentDidMount
  useEffect(() => {
    // create and add vector source layer
    const initalFeaturesLayer = new VectorLayer({
      source: new VectorSource(),
    });

    const seoul = [126.97794, 37.56629];
    const seoulWebMercator = fromLonLat(seoul);

    // create map
    const initialMap = new Map({
      target: mapElement.current!,
      layers: [

        // Google Maps Terrain
        new TileLayer({
          source: new XYZ({
            url: "http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}",
          }),
        }),

        initalFeaturesLayer,
      ],
      view: new View({
        projection: "EPSG:3857",
        center: seoulWebMercator,
        zoom: 16,
      }),
      controls: [],
    });

    // save map and vector layer references to state
    setMap(initialMap);
    setFeaturesLayer(initalFeaturesLayer);
  }, []);

  // update map if features prop changes - logic formerly put into componentDidUpdate
  useEffect(() => {
    if (props.features.length) {
      // may be null on first render

      // set features to map
      featuresLayer.setSource(
        new VectorSource({
          features: props.features, // make sure features is an array
        })
      );

      // fit map to feature extent (with 100px of padding)
      map.getView().fit(featuresLayer.getSource().getExtent(), {
        padding: [100, 100, 100, 100],
      });
    }
  }, [props.features, featuresLayer, map]);


  return (
    <div>
      <div ref={mapElement} className="map-container"></div>

    </div>
  );
};

export default Home;

【问题讨论】:

    标签: reactjs typescript use-state


    【解决方案1】:

    对于地图问题,您应该明确提供类型

    const [map, setMap] = useState<Map>();
    

    对于 feature 和 featureLayer 问题,你应该提供 props 类型

    type HomeProps = { features: any[] };
    
    const Home: FC<HomeProps> = (props) => {
    
     const [map, setMap] = useState<Map>();
     const [featuresLayer, setFeaturesLayer] = useState<
        VectorLayer<VectorSource<Geometry>>
     >();
    
    
     ...
    
     useEffect(() => {
        if (props.features?.length && featuresLayer) {
        ...
        }
     }, [props.features, featuresLayer, map]);
      
    }
    

    这里是codesandbox for demo

    【讨论】:

      【解决方案2】:

      在 TypeScript 中,您需要声明所有类型,包括对象类型中的所有属性。

      从你提供的错误信息来看,这两个错误是因为你没有声明正确的类型。

      “Map”类型的参数不能分配给“SetStateAction”类型的参数。类型 'Map' 与签名 '(prevState: undefined): undefined' 不匹配。

      这个错误是因为你没有为useState声明类型,所以默认为undefined。您可以通过执行以下操作轻松修复它:

      // You should substitute `any` type to a more appropriate type.
      const [map, setMap] = useState<Map<any, any>>();
      

      类型 '{ children?: ReactNode; 上不存在属性 'features' }'.`

      这个错误是因为你没有在组件props的类型中声明需要的属性。

      type HomeProps = {
        // You should substitute `any` type to a more appropriate type.
        features: any;
      };
      
      const Home: FC<HomeProps> = (props) => {
        ...
      }
      

      在您没有正确声明类型的情况下,可能还有更多类似的错误。您需要做的就是声明它们。

      【讨论】:

      • 您好,谢谢您的帮助。我添加了const [map, setMap] = useState&lt;Map&lt;any, any&gt;&gt;();,它修复了下面与 map/setMap 相关的错误。现在在我更改地图useState&lt;Map&lt;any, any&gt;&gt;(); 的状态时出现错误,说Type 'Map' is not generic. 我怎么知道会出现什么类型?
      猜你喜欢
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 2021-01-05
      • 2016-01-04
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多