Skip to content

Instantly share code, notes, and snippets.

@johntran
Last active December 30, 2015 11:23
Show Gist options
  • Save johntran/7565719b3c42109045f1 to your computer and use it in GitHub Desktop.
Save johntran/7565719b3c42109045f1 to your computer and use it in GitHub Desktop.
import { Component } from 'react'
import usersTable from './usersTable'
import fetch from 'fetch'
class UserTableContainer extends Component {
componentDidMount() {
/**
* When component is loaded on the page,
* get the user list from api and mutate state.
*/
fetch('/api/users')
.then(response => response.json())
// response = [{name: "John"}, {name: "Mary"}, {name: "Sherry"}]
.then(data => this.setState({users: data}))
/**
* Mutating state is bad, but since we haven't installed
* a data dispatcher like Flux/Redux for this example, it is ok.
*/
})
.catch(error => console.log('Error: ', error))
}
return (<usersTable users={this.state.users} />)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment