What is React.FC while using TS with React.js??

In TypeScript, React.FC is a generic type provided by the React library to define the type of a functional component. FC stands for "Functional Component." It is a shorthand for defining the type of React functional components, specifying the type of props that the component expects.

Here's a breakdown of React.FC:

  • React: The namespace provided by the React library.
  • FC: Stands for "Functional Component," indicating that this type is specifically for functional components.

When you see React.FC, it's typically used as a generic type with the angle brackets <...> to specify the type of props that the functional component receives. For example:


interface GreetingProps {

    name: string;

}

const Greeting: React.FC<GreetingProps> = ({ name }) => {

    return <div>Hello, {name}!</div>;

}        

In this example, GreetingProps is an interface defining the expected props for the Greeting component. When using React.FC<GreetingProps>, you're telling TypeScript that this is a functional component that takes props of type GreetingProps. This helps TypeScript provide better type checking and autocompletion when working with the component.

Note: While React.FC is commonly used, some developers prefer using React.FunctionComponent for better clarity, as they are functionally equivalent. Both React.FC and React.FunctionComponent can be used interchangeably.


Josiah Okornoe

Software Developer|?? Front End Developer?? | React Native | MERN Stack | Student

8 个月

Is it good to bring the React.FC if the component does not takes in any props?

回复

要查看或添加评论,请登录

Samiran Roy的更多文章

  • How can I declare object in typescript ?

    How can I declare object in typescript ?

    In TypeScript, you can declare an object by specifying its shape using an interface or a type. Here's an example using…

社区洞察

其他会员也浏览了