2

I am keeping my code short as possible to remove confusion, actually i am trying to convert my application built on React jsx to React Typescript thus (tsx file).

Error that i am receiving is - '[ts] Property 'state' does not exist on type 'App'.any ALSO same for '[ts] Property 'setState' does not exist on type 'App'.any' Please help me on this ...

interface STATE {
  loading: boolean
};

interface PROPS {};

export default class App extends Component<STATE, PROPS> {
  constructor(props:any) {
    super(props);
    this.state = {
      fruitsData : [],
      loading: false
    };
  }

  componentDidMount() {
    this.setState({
      loading: true
    });

    //Further functions present here plus call to service and binding the 
     data received to the array fruitsData
  }

My package.json

 {

  "name": "example",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "@types/classnames": "^2.2.3",
    "@types/node": "^4.0.35",
    "classnames": "^2.2.5",
    "gh-pages": "^0.12.0",
    "react": "^15.5.4",
    "react-dom": "^15.5.4",
    "react-scripts": "0.9.5",
    "typescript": "^2.7.0-insiders.20171214"
  },
  "dependencies": {
    "@types/react": "^16.0.34",
    "@types/react-dom": "^16.0.3",
    "awesome-typescript-loader": "^3.4.1",
    "react-search-box": "0.0.4"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build"
  }
}
3
  • you did install @types for react and ReactDOM right? Commented Jan 5, 2018 at 11:04
  • @Sujit.Warrier oops sorry not yet, both @types/react-dom & @types/react install are under progress Commented Jan 5, 2018 at 11:13
  • Unfortunately I'm not sure we have enough here to reproduce your issue. If you have the necessary modules installed, then I guess that the issue might come from TypeScript being incorrectly configured. Did you start from create-react-app? If so, there are guides on using it with TypeScript. Commented Jan 5, 2018 at 14:34

4 Answers 4

2

First of all you must install @types/{react packages}. Then you have to import React like that: import * as React from "react"

Then your code would look like this:

interface IAppState {
  fruitsData: any[]; // change it for your type
  loading: boolean;
}

interface IAppProps {
  // your props
}

class App extends React.Component<IAppProps, IAppState> {
  constructor(props: IAppProps) {
    super(props);
    this.state = {
      fruitsData: [],
      loading: false,
    };
  }

  public componentDidMount() {
    this.setState({
      loading: true;
    });
  }
}

or you can write it without constructor like this:

class App extends React.Component<IAppProps, IAppState> {
  public state: IAppState = {
    fruitsData: [],
    loading: false,
  }

  public componentDidMount() {
    this.setState({
      loading: true;
    });
  }
}

Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

1

First of all you should ensure that the type definitions for react and react-dom are installed. With NPM you could do:

npm install --save @types/{react,react-dom}

Then there are a few problems with your code, which I have corrected inline:

interface State {
  loading: boolean
  // add fruitsData to state
  fruitsData: string[] // or whatever type it is
}

interface Props {}

// Generic arguments are <Props, State>, not the other way around
export default class App extends Component<Props, State> {
  constructor(props: Props) { // use the correct type here
    super(props);
    this.state = {
      fruitsData : [],
      loading: false
    };
  }

5 Comments

i tried this but still i am getting the same error. But when i introduced 'public state: STATE;' after export default class App extends Component<PROPS, STATE>{}.. Error is not there on 'this.state' whereas on other side i am still getting the error '[ts] Property 'setState' does not exist on type 'App'
Yes i am importing it : "import React, { Component } from 'react' "
yes may be you are right but i made sure those are installed and present in package.json..actually after that also VSCode underlines them with same error ... i removed them by adding these two lines after Export default class App extends Component .. setState: any; public state: STATE;
If you are correctly extending React.Component<Props, State> then setState and state will be defined for you, so I guess that there's something missing still.
Thanks - @TomFenech for your help , i resolved the error by creating application again following NPM create-react-ts command :P
0

You were placing generic type PROPS & STATES at wrong position.

The first one is Props and second one is State.

export default class App extends Component<PROPS, STATE>

1 Comment

i tried this but still i am getting the same error.
0

1). run

npm install --save typescript @types/node @types/react @types/react-dom @types/jest

or

yarn add typescript @types/node @types/react @types/react-dom @types/jest

2). Rename any file to be a TypeScript file (e.g. src/index.js to src/index.tsx)

3). Restart your development server!

enjoy :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.