React TypeScript 使用 Array.prototype.map 迭代充满对象的数组

帕维尔

目前,我正在使用 React 进行我的第一个 TypeScript 项目。但是,我不知道 TypeScript,打算在以后某个时候专注于它的静态类型,同时在常规 JS 中准备整个结构。这个 React 应用程序连接到我的个人 API 并从那里获取数据。在常规 JS 中,我通常会在 useEffect 钩子中获取数据,并使用像这样的扩展运算符添加获取的数据setSomeData([...response.data]),但它警告我,Type any[] is not assignable to never[]所以我不得不这样做setSomeData(response.data);

第二个主要问题是,当我想从组件内部的数组填充此数据时,由于出现以下错误,我无法编译该项目 property make doesn't exist on type never

反应组件:


import { useEffect } from "react";

import carsService from "../services/cars.service";

function Main() {

  const [vehiclesList, setVehiclesList] = useState([]);


  useEffect(() => {
    carsService
      .getCars()
      .then((response) => {
        console.log(response.data);
        return setVehiclesList(response.data);
      })
      .catch((err) => console.error(err));
  }, []);

  return (
    <div className="main-container">
      <div className="makes-list">
        {vehiclesList.map((vehicle, index) => {
          return (
            <div key={index}>
              <p>{vehicle.make}</p>
            </div>
          );
        })}
      </div>
    </div>
  );
}

对象模型:

const carSchema = new Schema(
  {
    carId: { type: String, unique: true, require: true },
    make: { type: String, unique: true, require: true },
    models: [
      {
        name: { type: String, unique: true },
        reviews: [
          {
            Version: { type: String, require: true },
            Year: { type: Number, require: true },
            Engine: { type: String, requite: true },
            General: { type: String, require: true },
            Pros: { type: String, require: true },
            Cons: { type: String, require: true },
            User: { type: String, require: true },
            Date: { type: Date, default: Date.now },
          },
        ],
      },
    ],
  },
  {
    timestamps: {
      createdAt: "createdAt",
      updatedAt: "updatedAt",
    },
  }
);
幻灯片p2

vehiclesList.

import React from 'react';
import { useEffect, useState } from 'react';

import carsService from '../services/cars.service';

interface Review {
  Version: string;
  Year: number;
  Engine: string;
  General: string;
  Pros: string;
  Cons: string;
  User: string;
  Date: Date;
}

interface Model {
  name: string;
  reviews: Review[];
}

interface Car {
  carId: string;
  make: string;
  models: Model[];
  createdAt: string;
  updatedAt: string;
}

function Main() {
  const [vehiclesList, setVehiclesList] = useState<Car[]>([]);

  useEffect(() => {
    carsService
      .getCars()
      .then((response) => {
        console.log(response.data);
        return setVehiclesList(response.data);
      })
      .catch((err) => console.error(err));
  }, []);

  return (
    <div className="main-container">
      <div className="makes-list">
        {vehiclesList.map((vehicle, index) => {
          return (
            <div key={index}>
              <p>{vehicle.make}</p>
            </div>
          );
        })}
      </div>
    </div>
  );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法使用Array.map在React中渲染数组

Array.from与Array.prototype.map

Array.prototype.map()和Array.prototype.forEach()

Typescript可以使用使用元素类型参数的方法扩展Array.prototype吗?

使用Array.map(TypeScript)返回通用数组

在Javascript中使用Array.prototype.map()时删除项目

如何使用Array.prototype.includes()查找对象?

React.js:使用Map函数将对象数组转换成Array数组取决于指定的属性

使用Array.prototype.map异步等待'承诺{<pending>}

使用Array.prototype.includes定位对象数组中的属性

性能:findIndex与Array.prototype.map

在React中使用Array.Map()返回子数组

使用Array.prototype.map时将变量名称分配为对象属性

TypeScript const断言:如何使用Array.prototype.includes?

当被调用方数组与空数组联合时,typescript,Array.prototype.map()具有错误“表达式不可调用”

为什么Array.prototype.map会忽略稀疏数组中的空插槽,而Array.prototype.join不会呢?

使用.map()迭代对象React中的数组

我想更具体地了解有关在JavaScript和数组中使用Array.prototype.map()的信息

Array.prototype.map()无法正常工作

如何正确使用array.prototype.filter()而不是.map()

使用forEach,[]。forEach.call(...)或Array.prototype.slice.call(...)。forEach迭代类似数组的对象?

在Array.prototype.map()内部分解对象属性

如果不返回Array.prototype.map()是否正确使用

了解Array.prototype.map

如何使用 React.js 将 2D 数组传递到 array.prototype.map()

Array.prototype.map.call 与 Array.map

Map.prototype.forEach 不迭代 Map

仅使用 Array.prototype.map() 和 Array.prototype.filter() 达到以下效果

React/TypeScript .map over array 不呈现下拉选项