Skip to content

Instantly share code, notes, and snippets.

@wejendorp
Created April 21, 2017 08:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wejendorp/109c51ac18cd32adc955ead12080d186 to your computer and use it in GitHub Desktop.
Save wejendorp/109c51ac18cd32adc955ead12080d186 to your computer and use it in GitHub Desktop.
Codher mini todo
// Live coding results from Codher at Tradeshift
// 2017-04-20
//
// Usage:
// create-react-app codher
// cd codher
// paste this file to src/App.js
// npm start
//
import React, { Component } from 'react';
import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);
this.onKeyDown = this.onKeyDown.bind(this);
this.state = {
todo: 'Todo item',
todos: []
};
}
onChange(e) {
this.setState({
todo: e.target.value
});
}
onKeyDown(e) {
if(e.keyCode === 13) {
e.preventDefault();
const newTodos = this.state.todos
.concat([this.state.todo]);
this.setState({
todo: '',
todos: newTodos
});
}
}
componentWillMount() {
console.log('will mount', this);
}
render() {
return (
<form className="App">
<input
type="text"
placeholder="Type a thing"
onChange={this.onChange}
onKeyDown={this.onKeyDown}
value={this.state.todo}
/>
<ul>
{this.state.todos.map(todo => {
return <li>{todo}</li>;
})}
</ul>
</form>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment