Created
November 8, 2017 21:17
-
-
Save thebopshoobop/2b40c094c327b8b6a19047c3e9d122c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import ReactTable from "react-table"; | |
import "react-table/react-table.css"; | |
class App extends Component { | |
// Constructor is not needed if you don't need to compute initial state | |
state = { data: [] }; | |
indexData = (item, index) => ({ ...item, id: index + 1 }); | |
fetchPromise = url => { | |
return fetch(url) | |
.then(response => response.json()) | |
.then(data => data.results.map(this.indexData)); | |
}; | |
fetchAsync = async url => { | |
const response = await fetch(url); | |
const data = await response.json(); | |
return data.results.map(this.indexData); | |
}; | |
async componentDidMount() { | |
const url = "https://swapi.co/api/people"; | |
// Vanilla Promises | |
// this.fetchPromise(url) | |
// .then(data => this.setState({ data })) | |
// .catch(error => console.error(error)); | |
// Async Function | |
try { | |
const data = await this.fetchAsync(url); | |
this.setState({ data }); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
render() { | |
const columns = [ | |
{ Header: "#", accessor: "id" }, | |
{ Header: "Character Name", accessor: "name" }, | |
{ Header: "Character Gender", accessor: "gender" }, | |
{ Header: "Birth Year", accessor: "birth_year" } | |
]; | |
return ( | |
<div> | |
<h1>Table Demo</h1> | |
<ReactTable columns={columns} data={this.state.data} /> | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment