forked from academind/react-typescript-course-resources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.tsx
43 lines (36 loc) · 978 Bytes
/
List.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Example: A Generic List Component
// This reusable component can be used to render a list of items of any type. The type of the items is passed via a generic type parameter.
import { type ReactNode } from 'react';
type ListProps<T> = {
items: T[];
renderItem: (item: T) => ReactNode;
};
export function List<T>({ items, renderItem }: ListProps<T>) {
return <ul>{items.map(renderItem)}</ul>;
}
// Example Usage:
export function Demo() {
const users = [
{ id: 'u1', name: 'Max' },
{ id: 'u2', name: 'Manuel' },
];
const hobbies = ['Sports', 'Reading', 'Cooking'];
return (
<main>
<section>
<h2>Users</h2>
<List
items={users}
renderItem={(user) => <li key={user.id}>{user.name}</li>}
/>
</section>
<section>
<h2>Hobbies</h2>
<List
items={hobbies}
renderItem={(hobby) => <li key={hobby}>{hobby}</li>}
/>
</section>
</main>
);
}