typescript type for react component code example
Example 1: 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;
Example 2: 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 3: ts react props type
type Props = {
size: string;
}
const Component = ({ size = 'medium' }: Props) => (
<div className={cn('spinner', size)} />
);