Reactj, typescript The property 'setState' is not in the type

I get a ts error from my response component. The component works fine, builds, etc., however, typescript shows an error inside ide. I don’t know how I need to declare this in order to remove the error. I tried to create the setState method inside the component itself, but this gave even more errors.

Error: (15, 19) TS2605: JSX element type 'Home' is not a constructor function for JSX elements. The 'setState' property is not in the 'Home' type.

"typescript": "^ 2.3.4",

"respond": "^ 15.5.4",

"reaction-dom": "^ 15.5.4",

! ----

export class App extends React.Component<Props, State> {
  public state: State
  public props: Props

 constructor(props: Props) {
  super(props)
  this.state = {
    view: <Home />, <<<<
    } 

- the rest is removed for brevity

export class Home extends React.Component<Props, State> {
public state: State;
public props: Props;

constructor(props: Props) {
    super(props)

}
  public render() {
    return <h1>home</h1>
  }
}
+4
1

:

type HomeProps = {
    text: string;
}
class Home extends React.Component<HomeProps, void> {
    public render() {
        return <h1>{ this.props.text }</h1>
    }
}

type AppState = {
    homeText: string;
}
class App extends React.Component<void, AppState> {
    constructor() {
        super();

        this.state = {
            homeText: "home"
        };

        setTimeout(() => {
            this.setState({ homeText: "home after change "});
        }, 1000);
    }

    render() {
        return <Home text={ this.state.homeText } />
    }
}

, , .
, , DOM .

+1

Source: https://habr.com/ru/post/1679350/


All Articles