Skip to content

Commit 0f07e7b

Browse files
authored
Create README.md
1 parent df0bd7e commit 0f07e7b

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Furthermore by providing Interface declarations describing your API contracts yo
2525
- [React](#react)
2626
- [Stateless Component](#stateless-component)
2727
- [Class Component](#class-component)
28+
- [Generic Component](#generic-component)
2829
- [Higher-Order Component](#higher-order-component)
2930
- [Redux Connected Component](#redux-connected-component)
3031
- [Redux](#redux)
@@ -128,6 +129,67 @@ export default MyComponent;
128129

129130
---
130131

132+
## Generic Component
133+
- generic component boilerplate
134+
```tsx
135+
type GenericListProps<T> = {
136+
dataSource: T[],
137+
itemRenderer: (item: T) => JSX.Element,
138+
};
139+
140+
class GenericList<T> extends React.Component<GenericListProps<T>, {}> {
141+
render() {
142+
const { dataSource, itemRenderer } = this.props;
143+
144+
return (
145+
<div>
146+
{dataSource.map(itemRenderer)}
147+
</div>
148+
);
149+
}
150+
}
151+
```
152+
153+
```tsx
154+
interface IUser {
155+
id: string,
156+
name: string,
157+
}
158+
159+
type UserItemProps = {
160+
dataSource: IUser,
161+
};
162+
163+
cosnt UserItem: React.StatelessComponent<UserItemProps> = ({id, name}) => {
164+
return (
165+
<div>
166+
<b>{name}</b>
167+
<small>({id})</small>
168+
</div>
169+
);
170+
}
171+
```
172+
173+
```tsx
174+
const UserList = class extends Table<IUser> { };
175+
176+
const users = [{
177+
id: 'uuidv4',
178+
name: 'Dude',
179+
}];
180+
181+
ReactDOM.render(
182+
<UserList
183+
dataSource={users}
184+
itemRenderer={item => <UserItem dataSource={item} />}
185+
>
186+
</UserList>
187+
);
188+
189+
```
190+
191+
---
192+
131193
## Higher-Order Component
132194
- wrap and decorate input Component returning a new Component
133195
- new Component will inherit Props interface through composition from input Component extended with Props of `HOC`

0 commit comments

Comments
 (0)