Skip to content

Instantly share code, notes, and snippets.

@pokorson
Created June 18, 2017 20:16
Show Gist options
  • Save pokorson/c95333f71a59c7f01513643d358db0f3 to your computer and use it in GitHub Desktop.
Save pokorson/c95333f71a59c7f01513643d358db0f3 to your computer and use it in GitHub Desktop.
list class component
import React from "react";
class ListItem extends React.PureComponent {
render() {
return <li>{this.props.children}</li>;
}
}
class List extends React.PureComponent {
constructor(props) {
super(props);
this.state = { todos: [], name: "" };
this.addTodo = this.addTodo.bind(this);
this.updateName = this.updateName.bind(this);
}
addTodo() {
this.setState({
todos: [...this.state.todos, this.state.name],
name: ""
});
}
updateName(e) {
this.setState({
name: e.target.value
});
}
render() {
return (
<div>
<input onChange={this.updateName} value={this.state.name} />
<button onClick={this.addTodo}>add todo</button>
<ul>
{this.state.todos.map(t => <ListItem key={t}>{t}</ListItem>)}
</ul>
</div>
);
}
}
export default List;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment