Sub topic 5
React.FC or a plain function?
This chapter assumes you can write a React function component and know basic TypeScript generics. You have already typed props with an interface. Now you will choose how to attach that type to the component.
Two ways to write the same component
React.FC (short for React.FunctionComponent) is a generic type from @types/react. You annotate a variable with it, and TypeScript types the props and the return value for you.
import type { FC } from "react";
interface GreetingProps {
name: string;
}
export const Greeting: FC<GreetingProps> = ({ name }) => {
return <p>Hello, {name}</p>;
};A plain function annotates the parameter directly and lets TypeScript infer the return type.
interface GreetingProps {
name: string;
}
export function Greeting({ name }: GreetingProps) {
return <p>Hello, {name}</p>;
}Both compile, and callers use them the same way. The differences show up in edge cases.
Why `FC` is no longer the default choice
FC used to add an implicit children prop to every component. Code could read props.children without declaring it, and callers could pass children to components that ignored them. Version 18 of @types/react removed this behavior. Today FC adds nothing that your props interface does not already say, so you declare children: ReactNode yourself, as in the earlier chapter.
Other reasons teams prefer plain functions:
- Generics. An arrow function typed with
FC<...>cannot declare its own type parameter. A plain function can, which you need for generic components. - Less to read. The props type sits next to the parameter, so you see the contract where you destructure.
- Hoisting and naming. A
functiondeclaration is hoisted and gets its name for stack traces and React DevTools without extra work. - Inferred return type. TypeScript infers
JSX.Elementfor you. You can still annotate it when you want a stricter contract.
import type { ReactNode } from "react";
interface ListProps<T> {
items: T[];
renderItem: (item: T) => ReactNode;
}
export function List<T>({ items, renderItem }: ListProps<T>) {
return <ul>{items.map((item, i) => <li key={i}>{renderItem(item)}</li>)}</ul>;
}This works because T belongs to the function. With const List: FC<ListProps<T>> = ... there is no place to introduce T, and TypeScript reports that it cannot find the name.
Comparison
| Concern | `FC<Props>` | Plain function |
|---|---|---|
| Implicit `children` | No (removed in `@types/react` 18) | No |
| Generic components | Not directly | Yes |
| Return type | Checked against `ReactNode` | Inferred, or annotated by you |
| Static properties such as `displayName` | Typed on the component | Assigned after the function, checked as an expando property |
| Where the props type appears | On the variable | On the parameter |
When `FC` is still fine
FC is not an error. Many codebases use it, and a team that has chosen one style should stay consistent. It can also help when you assign a component to a variable that must match an existing component type, for example a map of components.
import type { FC } from "react";
const icons: Record<string, FC<{ size?: number }>> = {
star: ({ size = 16 }) => <svg width={size} height={size} />,
};For new code, the practical rule is to write plain functions and reach for FC only when you need to describe a component as a value.
Exercise
A teammate writes const Table: FC<TableProps<Row>> = ... and then wants the component to work with any row type. Why does FC get in the way, and what change fixes it?
Show answer
An arrow function typed with FC has no place to declare a type parameter, so T cannot be introduced. Rewrite it as function Table<T>({ rows }: TableProps<T>) { ... }, so T is declared on the function and inferred from the rows prop at each call site.