Created
December 17, 2017 04:25
-
-
Save Nicknyr/689a8923e2637865486cfc6775b53d53 to your computer and use it in GitHub Desktop.
Simple React To-Do list
This file contains 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'; | |
import './App.css'; | |
export default class ToDoList extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
userInput: '', | |
list: [] | |
} | |
} | |
// Sets the state onChange, passes input (e.target.value) to userInput | |
changeUserInput(input) { | |
this.setState({ | |
userInput: input | |
}); | |
} | |
addToList(input) { | |
// Makes a clone of list[] so we don't effect the original state | |
let listArray = this.state.list; | |
listArray.push(input); | |
// Sets our listArray variable equal to list[], resets userInput each time it's changed | |
this.setState ({ | |
list: listArray, | |
userInput: '' | |
}); | |
} | |
/* On rendering | |
On the click of the button whatever is typed in the input gets passed to addToList function | |
Ul maps through each elemet of list[] and passes them as val and creates an li for each one | |
*/ | |
render() { | |
return ( | |
<div className="to-do-list-main"> | |
<input | |
// When input text is changed this passes the change to changeUserInput | |
onChange={ (e) => this.changeUserInput(e.target.value)} | |
value={this.state.userInput} | |
type="text" | |
/> | |
<button onClick={ () => this.addToList(this.state.userInput)}>Press Me</button> | |
<ul> | |
{this.state.list.map( (val) => <li>{val}</li>)} | |
</ul> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment