Created
October 3, 2017 15:29
-
-
Save oampo/8fd5d18029a92005eda40ad43f645d2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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