Using state in react with TypeScript
Asked Answered
M

3

108

I am new to TypeScript. I've got a problem with displaying this.state.something inside the render method or assigning it to a variable inside a function.

Have a look at the most important piece of code:

interface State {
    playOrPause?: string;
}

class Player extends React.Component {
    constructor() {
        super();

        this.state = {
            playOrPause: 'Play'
        };
    }

    render() {
        return(
            <div>
                <button
                    ref={playPause => this.playPause = playPause}
                    title={this.state.playOrPause} // in this line I get an error
                    >
                    Play
                </button>
           </div>
        );
    }
}

The errors says: [ts] Property 'playOrPause' does not exist on type 'ReadOnly<{}>'.

I tried to declare the playOrPause property to be a type of string and it didn't work.

What am I missing here to make it work?

Maidamaidan answered 28/10, 2017 at 8:38 Comment(1)
Possible duplicate of 'ValueChanging' does not exist on type 'Readonly<{}>'Slovene
G
192

You need to declare that your component is using the State interface, it used by Typescript's Generics.

interface IProps {
}

interface IState {
  playOrPause?: string;
}

class Player extends React.Component<IProps, IState> {
  // ------------------------------------------^
  constructor(props: IProps) {
    super(props);

    this.state = {
      playOrPause: 'Play'
    };
  }

  render() {
    return(
      <div>
        <button
          ref={playPause => this.playPause = playPause}
          title={this.state.playOrPause} // in this line I get an error
        >
          Play
        </button>
      </div>
    );
  }
}
Gott answered 28/10, 2017 at 9:1 Comment(11)
Thanks. It works now. I tried to declare only IState interface and declare that class Player is using IState only (It still doesn't work then) But when I declare that empty interface called IProps together with IState then it works fine. Why is that?Maidamaidan
There is a meaning of the Generics position, the first position should indicate the props type, and the second is the state.Gott
if the state is declared outside the constructor state = { playOrPause: 'Play' } typescript doesn't validate it against IState. Why is it so?Coadjutrix
How outside of the constructor?Gott
@RahulYadav if you do that you can just do state: IState = {...} and drop it from the generic props. It will be inferred.Angell
In my case, I also need to add state: IState; before the constructor method, otherwise it still doesn't work.Noticeable
If you don't need the properties, you can omit the IProps interface and just write any instead: class Player extends React.Component<any, IState> (and don't forget to also replace it in the constructor).Birdt
@Gott What if there are nested propertiesCloaca
@Roster, such as?Gott
Like i fi have employee property insde state and that is of type Employee type and inside i have name property. So in Typescript how to define Employee type to state variable?Cloaca
The same, Interface IState { employee: Employee }, assuming that Employee is a classGott
M
53

In case anyone is wondering how to implement it in functional components with hooks ( not in a class):

const [value, setValue] = useState<number>(0);

useState is a generic function, that means that it can accept a type parameter. This type-parameter will tell TypeScript which types are acceptable for this state.

Mitchmitchael answered 5/6, 2021 at 15:44 Comment(1)
This is only valid for a function in react, not a class. I've learned this painful lesson.Trutko
S
4

In my case ( working with TypeScript, and the state value was actually a boolean ) I've had the same problem, I've fixed it by passing the state value I wanted to mark as output to String():

import React, { Component } from 'react';

interface ITestProps {
  name: string;
}

interface ITestState {
  toggle: boolean;
}

class Test extends Component<ITestProps, ITestState> {
  constructor(props: ITestProps) {
    super(props);

    this.state = {
      toggle: false,
    };

    this.onClick = this.onClick.bind(this);
  }

  onClick() {
    this.setState((previousState, props) => ({
      toggle: !previousState.toggle,
    }));
  }

  render() {
    return (
      <div>
        Hello, {this.props.name}!
        <br />
        Toggle state is: {String(this.state.toggle)}
      </div>
    )
  }
}
Software answered 2/9, 2020 at 22:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.