Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript + React - No overload matches this call

I am kind of new with React + Typescript and I'm trying to update the four counters that I have created depending on what their value is. Every counter has a key that is connected to their id inside an array as you can see.

However when I try to add: value={counter.value} so that the value will update and not start at zero every time, then I get this message:

" 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; }>'. "

I know that there is a question that has been answered that is very similar to this, but I really didn't understand the answer or how to fix it. Thank you in advance!

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;

Under here is the Counter component:

    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
    }
};
like image 300
Christofer Carlsson Avatar asked Sep 02 '25 04:09

Christofer Carlsson


1 Answers

In tsx, type of props (not to be confused with propTypes) need to be explicitly defined when extending React.Component, otherwise the type {} will be assumed as the props.

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

hence,

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

is required in order for Counter component to be passed by value attribute

<Counter value={4} />

key props is not required because it's already built in as an optional attribute to React.Components

like image 161
Alan Darmasaputra Avatar answered Sep 05 '25 01:09

Alan Darmasaputra