Generic Components

Last updated: 2026-11-02

Generic Components Generic Components

Generic components let the type follow the data: a function List<T> renders an array of strings with T=string and an array of objects with T=User. You write it once and every element type gets full inference and hints, which is the core technique behind reusable UI libraries and data-layer wrappers. The component logic stops caring about the shape of the items, so the same code serves any payload the caller hands over.

GroupData & Engineering
KindPattern

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

type ListProps<T> = {
  items: T[]
  renderItem: (item: T) => React.ReactNode   // the caller decides how to render
  keyOf?: (item: T) => string | number
}

function List<T>({ items, renderItem, keyOf }: ListProps<T>) {
  return (
    <ul>
      {items.map((it, i) => (
        <li key={keyOf ? keyOf(it) : i}>{renderItem(it)}</li>
      ))}
    </ul>
  )
}

// T is inferred automatically at the call site:
<List items={['a', 'b']} renderItem={s => s.toUpperCase()} />      // T = string
<List items={users} renderItem={u => u.name} keyOf={u => u.id} />   // T = User

Returns:No return value; this is a coding pattern rather than a callable API.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Three lists reuse the same GenericList component to render strings, numbers and objects - generics decouple the component logic from the element type (runtime JSX demo, the types are inferred in the TypeScript version).

💡 Key Points

💡 Tip Constrain a generic with <T extends { id: number }>: the field has to exist and T keeps its concrete type, which is far safer than any.
ℹ️ Info Typical generic components: a table Table<T>, a select Select<T> and an API layer where request<T>() returns Promise<T>.
⚠️ Warning The generic <T> of an arrow function is parsed as a JSX tag, so write <T,> with a trailing comma or use a function declaration instead.
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q At which step is the type of a generic component determined?
A At the call site. Pass items={['a']} and TypeScript fixes T to string, so the parameters and the return type of renderItem are checked against it from then on.
Q What is the real difference to any?
A any gives up the checking and loses hints on both ends, while a generic keeps the "unknown but consistent" relation: the type you pass in is the type you get out.
Q When do I really need a generic component?
A Most often in reusable UI libraries (tables, forms, pickers) and in data-layer wrappers; a component you write twice and throw away does not need to be generic.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏