0
Q:

react typescript props

import React from 'react';

interface Props {
}

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

export default App;
4
class Test extends Component<PropsType,StateType> {
  constructor(props : PropsType){
    	super(props)
  }
  
  render(){
   	console.log(this.props) 
    return (
     	<p>this.props.whatever</p> 
    )
  }
  
};
2
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>
    );
  }
}
1
// Declare the type of the props
type CarProps = {
  name: string;
  brand: string;
  price;
}

// usage 1
const Car: React.FC<CarProps> = (props) => {
  const { name, brand, price } = props;
  // some logic
}

// usage 2
const Car: React.FC<CarProps> = ({ name, brand, price }) => {
	// some logic
}

0

New to Communities?

Join the community