Skip to content

Commit bd6703c

Browse files
committed
Generics
1 parent ea87073 commit bd6703c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

react-typescript-demo/src/App.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { DomRef } from './components/refs/DomRef'
1515
import { MutableRef } from './components/refs/MutableRef'
1616
import './App.css'
1717
import { Counter } from './components/class/Counter'
18+
import { List } from './components/generics/List'
1819

1920
function App() {
2021
const personName = {
@@ -63,6 +64,28 @@ function App() {
6364
<DomRef />
6465
<MutableRef />
6566
<Counter message='The count value is ' />
67+
<List
68+
items={['Batman', 'Superman', 'Wonder Woman']}
69+
onClick={item => console.log(item)}
70+
/>
71+
<List items={[1, 2, 3]} onClick={item => console.log(item)} />
72+
<List
73+
items={[
74+
{
75+
first: 'Bruce',
76+
last: 'Wayne'
77+
},
78+
{
79+
first: 'Clark',
80+
last: 'Kent'
81+
},
82+
{
83+
first: 'Princess',
84+
last: 'Diana'
85+
}
86+
]}
87+
onClick={item => console.log(item)}
88+
/>
6689
</div>
6790
)
6891
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
type ListProps<T> = {
2+
items: T[]
3+
onClick: (value: T) => void
4+
}
5+
6+
export const List = <T extends { id: number }>({
7+
items,
8+
onClick
9+
}: ListProps<T>) => {
10+
return (
11+
<div>
12+
<h2>List of items</h2>
13+
{items.map((item, index) => {
14+
return (
15+
<div key={item.id} onClick={() => onClick(item)}>
16+
{item}
17+
</div>
18+
)
19+
})}
20+
</div>
21+
)
22+
}

0 commit comments

Comments
 (0)