react typescript props code example
Example 1: react tsx component example
import React from 'react';
interface Props {
}
const App: React.FC<Props> = (props) => {
return (
<div><div/>
);
};
export default App;
Example 2: react typescript props
type CarProps = {
name: string;
brand: string;
price;
}
const Car: React.FC<CarProps> = (props) => {
const { name, brand, price } = props;
}
const Car: React.FC<CarProps> = ({ name, brand, price }) => {
}
Example 3: typescript react elements
let element: JSX.Element;
Example 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>
)
}
};
Example 5: 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}
>
Play
</button>
</div>
);
}
}
Example 6: ts react props type
type Props = {
size: string;
}
const Component = ({ size = 'medium' }: Props) => (
<div className={cn('spinner', size)} />
);