Skip to content

Instantly share code, notes, and snippets.

@oampo
Created October 3, 2017 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oampo/8fd5d18029a92005eda40ad43f645d2d to your computer and use it in GitHub Desktop.
Save oampo/8fd5d18029a92005eda40ad43f645d2d to your computer and use it in GitHub Desktop.
import React from 'react';
export default function AddForm(props) {
return (
<form onSubmit={e => props.onSubmit(e)}>
<label htmlFor="textInput">Item to add:</label>
<input
id="textInput"
type="text"
onChange={e => props.onChange(e)}
value={props.inputText}
/>
<button>Add item</button>
</form>
);
}
import React from 'react';
import TodoList from './todo-list';
import AddForm from './add-form';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
items: ['Buy milk', 'Make dinner', 'Fix broadband'],
inputText: 'Go for a walk'
};
}
addItem(e) {
e.preventDefault();
this.setState({
items: [...this.state.items, this.state.inputText],
inputText: ''
});
}
render() {
return (
<div className="todo-list-app">
<TodoList items={this.state.items} />
<AddForm
inputText={this.state.inputText}
onChange={e => this.setState({inputText: e.target.value})}
onSubmit={e => this.addItem(e)}
/>
</div>
);
}
}
import React from 'react';
export default function TodoList(props) {
const itemList = props.items.map((item, index) => (
<li key={index}>{item}</li>
));
return <ul className="todo-list">{itemList}</ul>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment