react functional components typescript 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 functional component typescript
import React from 'react';
interface Props {
}
export const App: React.FC<Props> = (props) => {
return (
<>
<SomeComponent/>
</>
);
};
Example 3: 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 4: How to use Function Components in React TypeScript
import React from "react";
type Props = {
linkFirst: string,
routeFirst: string,
head?: string,
linkSecond?: string,
routeSecond?: string
};
const DefaultProps = {
head: "Navbar Head not found"
};
const Navbar: React.FC <Props> = (props) => {
return (
<div>
<nav>
{ props.head }
<ul>
<li>
<a href={ props.routeFirst }> {props.linkFirst} </a>
<li>
<a href= { props.routeSecond }> {props.linkSecond} </a>
</li>
</ul>
</nav>
</div>
);
};
Navbar.defaultProps = DefaultProps;
export default Navbar;