# React Component Typing

> Building bulletproof React components.

Subject: TypeScript | Lesson 51 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/react-component-typing
Previous: [Typing the DOM](https://codewithanimation.in/typescript/typing-the-dom.md) | Next: [React Hooks Typing](https://codewithanimation.in/typescript/react-hooks-typing.md)

## Typing Props

In modern React, we use TypeScript `interface` or `type` definitions to strictly define what props a component accepts.

But the real magic happens when you use **Generics** to create polymorphic components (like a Table that can render *any* data type while keeping strict type safety).

## The Reusability Problem

If you build a `<Table>` component typed specifically for a `User[]`, you can't reuse it for a `Product[]`. If you type the data as `any[]`, you lose all autocompletion when trying to render a row.

By passing a type variable `<T>` to the component props, the Table dynamically molds itself to whatever array you pass into it.

## Syntax

```tsx
import { ReactNode } from "react";

interface TableProps<T> {
  data: T[];
  renderRow: (item: T) => ReactNode;
}

// Notice the trailing comma <T,> - it prevents the 
// JSX parser from confusing it with an HTML tag!
export function Table<T,>({ data, renderRow }: TableProps<T>) {
  return (
    <div>{data.map((item, i) => renderRow(item))}</div>
  );
}
```

## Common mistake: The JSX Generic Trap

In standard `.ts` files, you can write an arrow function generic as `const fn = <T>(arg: T) => {}`.

If you try this in a `.tsx` file, the parser thinks `<T>` is an unclosed JSX element and throws a massive error. You must write `<T,>` or `<T extends unknown>` to force it to parse as a generic.

## Tip: React.FC

You might see old tutorials using `const MyComp: React.FC<Props> = ...`. This is now considered an anti-pattern. Just type your arguments directly: `function MyComp({ name }: Props) {}`.

## Try it: Try it

Pass different data arrays to the generic `<Table>` component and watch how TypeScript instantly infers the correct types for the `renderRow` callback prop.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/react-component-typing

## Check yourself

### Question 1

Why must you sometimes write `<T,>` when defining a generic arrow function in a `.tsx` file?

- A. To indicate that the generic takes multiple arguments.
- B. To prevent the JSX parser from mistaking the generic for an HTML/JSX tag.
- C. To define a generic that defaults to null.

Correct answer: B

JSX syntax `<Tag>` heavily conflicts with generic syntax `<Type>`. Adding a comma `<Type,>` disambiguates it for the parser.

## Remember this

- Use Generics `<T>` to create highly reusable React components.
- A generic `<Table data={users}>` will automatically infer that `T` is `User`.
- In `.tsx` arrow functions, use `<T,>` to avoid JSX parsing errors.
- Stop using `React.FC`—type your function parameters directly.
