Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DarkoTrpevski/9eec0bc7d0a732ad45fc619a095500dc to your computer and use it in GitHub Desktop.
Save DarkoTrpevski/9eec0bc7d0a732ad45fc619a095500dc to your computer and use it in GitHub Desktop.
//Sample Array Data
const sampleData = [
{ name: "Bill", score: 15 },
{ name: "Jane", score: 18 },
{ name: "Fred", score: 10 }
]
<List data = {sampleData} onRowClick={row => console.log(row)} />
import React from 'react'
interface Props<T> {
data: T[];
onRowClick: (item: T) => void;
}
const List = <T extends object>({ data, onRowClick }: Props<T>) => {
if(data.length === 0) return null;
return (
<table>
<thead>
Some Title/Head
</thead>
<tbody>
{
data.map((item: T, index: number) => (
<tr key={index} onClick={() => onRowClick(item)}>
{
(Object.keys(data[0]) as Array<keyof T>).map(key => (
<td key={key.toString()}>{item[key]}</td>)
)
}
</tr>
))
}
</tbody>
</table>
);
}
export default List;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment