reactjs - Typescript + React - 没有重载匹配这个调用

标签 reactjs typescript key

我是 React + Typescript 的新手,我正在尝试根据它们的值更新我创建的四个计数器。 如您所见,每个计数器都有一个键,该键连接到数组中的 id。

但是,当我尝试添加:value={counter.value} 以便值会更新而不是每次都从零开始时,我收到此消息:

" No overload matches this call.
  Overload 1 of 2, '(props: Readonly<{}>): Counter', gave the following error.
    Type '{ key: number; value: number; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Counter> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'.
      Property 'value' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<Counter> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'.
  Overload 2 of 2, '(props: {}, context?: any): Counter', gave the following error.
    Type '{ key: number; value: number; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Counter> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'. "

我知道已经回答了一个与此非常相似的问题,但我真的不明白答案或如何解决它。 提前致谢!

import * as React from "react";
import { Component } from "react";
import Counter from "./Hello";

class Counters extends Component {
  state = {
    counters: [
      { id: 1, value: 4 },
      { id: 2, value: 0 },
      { id: 3, value: 0 },
      { id: 4, value: 0 }
    ]
  };
  render() {
    return (
      <div>
        {this.state.counters.map(counter => (
          <Counter key={counter.id} value={counter.value} />
        ))}
      </div>
    );
  }
}

export default Counters;

下面是计数器组件:

    import * as React from 'react';


export default class Counter extends React.Component {
    state = {
        count: 0,
        tags: [] as number[]
    };

    renderTags() {
        if (this.state.tags.length === 0) return <p>There are no tags</p>

        return(
            <ul>
                {this.state.tags.map(tag => <li key={tag}>{tag}</li>)}
            </ul>
        )
    }

    handleIncrement = (product:any) =>{
        this.setState({ count: this.state.count +1 })
        console.log("increment clicked", this.state.count);

    }

    doHandleIncrement = () => {
        this.handleIncrement({id:1});
    }

    render() { 

        return (

        <div>
            <span style={this.styles} className={this.getClassName()}>{this.formatCount()}</span>
            <button onClick={this.doHandleIncrement}>Test</button>
        </div>
        );
    }

    private getClassName() {
        let classes = "state";
        classes += (this.state.count === 0) ? "Warning" : "Normal";
        return classes;
    }

    private formatCount() {
        const {count} = this.state;
        return count === 0 ? "Zero" : count
    }
};

最佳答案

在 tsx 中,在扩展 React.Component 时需要显式定义 props 类型(不要与 propTypes 混淆),否则 {} 类型将被假定为 props。

React.Component<Props = {}, State = {}>

因此,

export default class Counter extends React.Component<{ value:number }>{ ... }

Counter组件被value属性传递所必需的

<Counter value={4} />

key Prop 不是必需的,因为它已经作为 React.Components 的可选属性内置

关于reactjs - Typescript + React - 没有重载匹配这个调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60490456/

相关文章:

javascript - 在 react-router-dom 中将 id 作为 props 传递

javascript - ESLint 希望 setSate 作为 useEffect 的依赖项,但这会导致无限循环(react-hooks/exhaustive-deps)

reactjs - 如何用 jest 和 typescript 测试一个基本的 react 功能组件

kendo-ui - 将 Kendo UI 与 TypeScript 0.9.1 结合使用会导致 Kendo 的定义文件中出现许多错误

javascript - 使用带有 .key 名称的键名访问 JSON 对象时出现问题但适用于 []

ssh - 访问被拒绝 `none' ...Windows 上的 ssh 错误

javascript - NextJS - onClick 时调用 js 函数

angular - 在 Angular 9 中如何在 http 客户端响应模型中强制属性日期类型?

javascript - 相同版本的 Node 但不同的模块版本

spring-mvc - Redis使用keys查询keys,CPU跑高20%以上,我只用单台redis