Skip to content

Instantly share code, notes, and snippets.

@fresh5447
Created July 26, 2017 19:05
Show Gist options
  • Save fresh5447/477d2be5085aa542c7af3998b4c7d89f to your computer and use it in GitHub Desktop.
Save fresh5447/477d2be5085aa542c7af3998b4c7d89f to your computer and use it in GitHub Desktop.
Sample for users list.
import React from 'react';
import myData from './data/data'
// SMART / Container / has state
// DUMB / Presentational / return HTML
// this.setState({})
const UsersList = (props) => {
return (
<div>
<h1> Found Users List </h1>
<ul>
{
props.usersData.map((user, ind) => {
return <li key={ind}> {user.name} </li>
})
}
</ul>
</div>
)
}
class Playground extends React.Component {
state = {
name: "Doug",
counter: 0,
users: null,
}
componentDidMount() {
this.fetchUsersFromServer()
}
fetchUsersFromServer() {
// pretend it's an AJAX function
const users = [
{ name: "Doug", id: 1, },
{ name: "Hannah", id: 2, },
{ name: "Jette", id: 3, },
{ name: "Joseph", id: 4, },
]
setTimeout(() => {
this.setState({ users: users })
}, 3000);
}
increaseCount(){
this.setState({ counter: this.state.counter += 1})
}
decreaseCount(){
this.setState({ counter: this.state.counter -= 1})
}
render() {
return (
<div>
<h1> My name is: {this.state.name} </h1>
<h3> counter: {this.state.counter} </h3>
<button onClick={() => this.increaseCount() }
> Increase Counter </button>
<button onClick={
() => this.decreaseCount() }
> Decrement Counter </button>
{
this.state.users
? <UsersList usersData={this.state.users} />
: <h1> Users Being Loaded </h1>
}
</div>
)
}
}
export default Playground;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment