Generic React Components and Hooks in TypeScript

Type a reusable List, Select or Table component with generics, write generic hooks, and the .tsx arrow-function syntax that trips people up.

In this chapter

Generic React components and hooks

More on Generics

Keep learning

Overview

chapters · generics Generic React components and hooks A typed list, select and fetch hook where the item type flows from props into callbacks.

1. A reusable list that types nothing

A shared List takes any[] so it works for dishes, orders and chats. any means “switch type checking off for this value”. So a typo in a property name is never caught. It just shows a blank. items: any[] d: any Compiles, because d is any and nothing is checked. d.nmae is undefined at runtime, so the menu shows empty rows. items: T[] d: Dish Property 'nmae' does not exist on type 'Dish'.

2. A generic List<T>

inferred from the prop d: Dish The type flows from one prop into the others' callbacks. Type 'number' is not assignable to type 'string'.

3. Arrow components in .tsx: write <T,>

JSX element 'T' has no corresponding closing tag. The parser reads <T> as an opening JSX tag, then looks for a </T> that never comes. The trailing comma says “type parameter list”. <T extends unknown> “extends unknown” means “T can be any type”, which a bare T already means. The extra word just ends the JSX guess.

4. Select<T>: value and onChange share T

A select shows options, marks one value as chosen, and calls onChange with the new pick. All three should use the same type T. What is next? Two props vote on T: options says Size, value says "M". "M" was typed right here as a literal, so TypeScript widens the result to string. next gets T, so it is string. With NoInfer<T> on value, only options votes, and next is Size. How next becomes string (no NoInfer) value="M" gives a second candidate one candidate is a literal written inline, so TypeScript widens onChange gets (next: T) next: string with value: NoInfer<T> only options decides T Type '"XL"' is not assignable to type 'Size'. "XL" is not one of 'S' | 'M' | 'L'. you write T yourself in <…> after the name, so nothing is inferred

5. Generic hooks

Dish[] | undefined T comes from a parser

useLocalStorage: T from the initial value

theme: string mode: 'dark' | 'light' Argument of type '"blue"' is not assignable to parameter of type '"dark" | "light"'. Line 5: T comes from 'dark' and widens to string, so any string is allowed later. Line 6 passes T by hand in <…>. Now only 'dark' or 'light' fit, so 'blue' on line 7 is rejected. useLocalStorage('theme', 'dark') the literal 'dark' widens to string useLocalStorage('cart', []) an empty [] has no items to learn from. never is the empty type (no value fits), so you can never add an item. Pass <CartItem[]>. useLocalStorage<CartItem[]>('cart', [])

6. Caveat: forwardRef drops generics

forwardRef (React 18's way to pass a ref through) returns a non-generic component type. It has no <T> slot left. So callers get T as unknown (“could be anything”), and renderItem loses its Dish type. React 18 workaround React 19

Remember

In .tsx, write arrow generics as <T,>. Use NoInfer on props that should follow, not lead. ! Cheat code items: T[] leads, callbacks follow. Write <T,> in .tsx. NoInfer on value. items → renderItem, getKey options → value, onChange → Deeper: NoInfer and inference sites, polymorphic components, type a fetch wrapper, generics as functions.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • React patterns from open source — Patterns extracted from Material UI, Radix, React Aria, React Hook Form, Redux Toolkit, TanStack, and Next.js — not slides about hooks.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.