Last active
June 7, 2018 06:14
-
-
Save adithyamaheshb/072567e3be24669b78b8bec75f6c5c61 to your computer and use it in GitHub Desktop.
Basic React Component for a ToDo List
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, {Component} from 'react'; | |
const List = props => ( | |
<ul> | |
{ | |
props.itmes.map((item, index) => <li key={index}>{item}</li>) | |
} | |
</ul> | |
); | |
export default class App extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
input: '', | |
items: [] | |
}; | |
this.onChange = this.onChange.bind(this); | |
this.onSubmit = this.onSubmit.bind(this); | |
} | |
onChange(e) { | |
this.setState({ | |
input: e.target.value | |
}); | |
onSubmit(e) { | |
e.preventDefault(); | |
this.setState({ | |
input: '', | |
items: [...this.state.items, this.state.input] | |
}); | |
} | |
render() { | |
return( | |
<div> | |
<form className="App" onSubmit={this.onSubmit}> | |
<input value={this.state.input} onChange={this.onChange} /> | |
<button>Submit</button> | |
</form> | |
<List items={this.state.items} /> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment