Answers for "new set typescript react"

1

react typescript set type

interface MyFooType {
    value: string;
}

const [foo, setFoo] = useState<MyFooType>(); // if not array

const [foo, setFoo] = useState<MyFooType[]>([]); // if it's an array
Posted by: Guest on February-24-2022
2

state in react typescript

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>
    );
  }
}
Posted by: Guest on July-14-2020

Code answers related to "TypeScript"

Browse Popular Code Answers by Language