Skip to content

Instantly share code, notes, and snippets.

@jklapacz
Created March 21, 2019 15:34
Show Gist options
  • Select an option

  • Save jklapacz/83db77e9bd914b04d61da42d8bb8a491 to your computer and use it in GitHub Desktop.

Select an option

Save jklapacz/83db77e9bd914b04d61da42d8bb8a491 to your computer and use it in GitHub Desktop.
Table example
import React, { useEffect, useState } from 'react';
import { Button } from '@blueprintjs/core';
import { Cell, Column, Table } from '@blueprintjs/table';
import axios from 'axios';
const cellRenderer = (data: Driver[] | undefined) => (rowIndex: number) => {
if (data && rowIndex) {
console.log('hey', data, rowIndex, data[rowIndex]);
return <Cell>{data[rowIndex]}</Cell>;
} else {
return <Cell>'-'</Cell>;
}
};
const apiUrl = 'http://localhost:3000';
function MyTable() {
const [data, setData] = useState<User[]>();
useEffect(() => {
const fetchData = async () => {
const response = await axios.get<User[]>(`${apiUrl}/users`);
setData(response.data);
return () => {
console.log('hi');
};
};
fetchData();
}, []);
return (
<>
<Button>Refresh</Button>
{console.log('data', data)}
<Table numRows={5}>
<Column name="Test" cellRenderer={cellRenderer(data)} />
<Column />
<Column />
</Table>
</>
);
}
interface User {
id: string;
name: string;
}
export default MyTable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment