Answers for "typescript react class component"

8

react tsx component example

import React from 'react';

interface Props {
}

const App: React.FC<Props> = (props) => {
  return (
    <div><div/>
  );
};

export default App;
Posted by: Guest on September-15-2020
2

typescript react class component

import * as React from 'react';

export interface TestProps {
    count: number;
}
 
export interface TestState {
    count: number;
}
 
class Test extends React.Component<TestProps, TestState> {
    state = { count:0  }
    render() { 
        return ( <h2>this is test</h2> );
    }
}
 
export default Test;
Posted by: Guest on May-08-2021
1

typescript react elements

let element: JSX.Element;
let element: TSX.Element;
Posted by: Guest on December-26-2020
3

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
4

TYPESCript props class component

class Test extends Component<PropsType,StateType> {
  constructor(props : PropsType){
    	super(props)
  }
  
  render(){
   	console.log(this.props) 
    return (
     	<p>this.props.whatever</p> 
    )
  }
  
};
Posted by: Guest on July-12-2020
-1

how to use class component in typescript react

// Class Component React TypeScript ( with props and state usage ):

import React, { Component } from 'react';

type Props = { head ?: string , age ?: number };

type State = { num: number  };

type DefaultProps = { head : string, age : string }
/* <Props, State, DefaultProps> tell that 
these initialised types will be used in the class. */
class Counter extends Component <Props, State, DefaultProps> {

/* Default props appear when no value for the prop 
is present when component is being called */

    
    static defaultProps : DefaultProps  = {
        head: "Heading is Missing",
        age: "Age is missing",
    };
// Always write state above the render() method.
    state: State = {
        num: 0,
    };

    render() {
  
        const add = () => this.setState({ num: this.state.num + 1 }) ;
        
        return (
            <div>
                <h1> { this.props.head } </h1>
                <p> Age: { this.props.age } </p> <br />
                <h3> { this.state.num } </h3>
                <button onClick={add}>Add 1</button>
            </div>
        );
    }
}

export default Counter;
Posted by: Guest on May-27-2021

Code answers related to "typescript react class component"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language