简体   繁体   English

React/Typescript 属性“ID”在类型“never”上不存在。ts(2339)

[英]React/Typescript Property 'ID' does not exist on type 'never'.ts(2339)

I have this React/Typescript page:我有这个 React/Typescript 页面:

PlayersPage.tsx玩家页面.tsx

import React, { useState, useEffect } from 'react';
import { Page } from './Page';
import { PageTitle } from './PageTitle';
import { Button, Card } from 'react-bootstrap';

function PlayersPage() {
  const [isLoading, setIsLoading] = useState(false);
  const [playersData, setPlayersData] = useState([]);

  //Fetch all players from database
  useEffect(() => {
    //setIsLoading(true);
    fetch('http://localhost:44326/api/Players')
      .then((response) => {
        if (response.ok) {
          return response.json();
        }
      })
      .then((data) => setPlayersData(data));
    //.then(setIsLoading(false));
  }, []);

  return (
    <Page>
      <div style={{ padding: 20 }}>
        <PageTitle>Toons</PageTitle>
      </div>

      <div>
        {playersData.map((data) => (
          <div key={data.ID}>
            <Card>
              <Card.Title>{data.ID}</Card.Title>
              <Card.Text>{data.Handle}</Card.Text>
              <Card.Body>
                <div className="lables">
                  <Card.Text>Role</Card.Text>
                </div>
                <div className="lables">
                  <Card.Text>{data.Role}</Card.Text>
                </div>
                <Button variant="primary">Work</Button>
              </Card.Body>
            </Card>
            <br></br>
          </div>
        ))}
      </div>
    </Page>
  );
}

export default PlayersPage;

I am getting this error: Property 'ID' does not exist on type 'never'.ts(2339) on every line that has data.ID or data.whatever .我收到此错误:在具有data.IDdata.whatever的每一行Property 'ID' does not exist on type 'never'.ts(2339)

Why would those values never be used or are always null?为什么这些值永远不会被使用或者总是 null? I use nearly identical code in a couple other apps so I'm not sure what is different about this situation.我在其他几个应用程序中使用了几乎相同的代码,所以我不确定这种情况有什么不同。 Thanks in advance.提前致谢。

You need to add type for playersData when you decalre it.您需要在贴花时为playersData添加类型。

interface DataType {
  ID: string;
  Handle: string;
  Role: string;
}

const [playersData, setPlayersData] = useState<Array<DataType>>([]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typescript 错误:属性“status”在类型“never”上不存在。ts(2339) - Typescript error: Property 'status' does not exist on type 'never'.ts(2339) TS2339:类型“ {}”上不存在属性“焦点”。 使用React打字稿 - TS2339: Property 'focus' does not exist on type '{}'. with React typescript 类型'A'.ts(2339)上不存在属性'ref' - React,TypeScript - Property 'ref' does not exist on type 'A'.ts(2339) - React, TypeScript 从 React.js 转换为 React.tsx 属性“id”在类型“never”上不存在。 TS2339 - Converting from React.js to React.tsx Property 'id' does not exist on type 'never'. TS2339 类型“never[]”.ts(2339) 上不存在属性“level”,但它确实记录了 - Property 'level' does not exist on type 'never[]'.ts(2339) but it does log TypeScript 错误:属性“scrollIntoView”在类型“never”上不存在。 TS2339 - TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339 React Navigation w/ TypeScript:如何正确输入屏幕参数? 类型 object.ts(2339) 上不存在属性 ID - React Navigation w/ TypeScript: How to properly type out screen params? Property id does not exist on type object.ts(2339) TS2339:“从不”类型上不存在属性“getBoundingClientRect” - TS2339: Property 'getBoundingClientRect' does not exist on type 'never' “TypedResponse”类型上不存在属性<never> '.ts(2339)</never> - Property does not exist on type 'TypedResponse<never>'.ts(2339) 类型“从不”上不存在属性“单击”。 TS2339 - Property 'click' does not exist on type 'never'. TS2339
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM