Last active
August 3, 2019 08:48
-
-
Save dorelljames/5b2709fd49f4927e00253f621b6aae12 to your computer and use it in GitHub Desktop.
App.js
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 from "react"; | |
import axios from "axios"; | |
class App extends React.Component { | |
constructor() { | |
super(); | |
this.state = { | |
todos: [], | |
isFetching: true | |
}; | |
} | |
componentDidMount() { | |
fetch("https://testreacttodoapp.herokuapp.com/todos") | |
.then(response => response.json()) | |
.then(todos => | |
this.setState({ | |
todos: todos, | |
isFetching: false | |
}) | |
); | |
} | |
handleSubmit = e => { | |
const newTodo = e.target.querySelector("input").value; | |
e.target.querySelector("input").value = ""; | |
axios | |
.post("https://testreacttodoapp.herokuapp.com/todos", { | |
title: newTodo | |
}) | |
.then(response => response.data) | |
.then(addedTodo => | |
this.setState({ | |
todos: [ | |
...this.state.todos, | |
{ | |
id: addedTodo.id, | |
title: addedTodo.title | |
} | |
] | |
}) | |
); | |
e.preventDefault(); | |
}; | |
handleDelete(id) { | |
axios | |
.delete("https://testreacttodoapp.herokuapp.com/todos/" + id) | |
.then(response => response.data) | |
.then(deletedTodo => { | |
this.setState({ | |
todos: this.state.todos.filter(todo => todo.id !== deletedTodo.id) | |
}); | |
}); | |
} | |
render() { | |
return ( | |
<div> | |
<h2>Todo list</h2> | |
<form onSubmit={this.handleSubmit}> | |
<input type="text" placeholder="What needs to be done?" /> | |
</form> | |
{this.state.isFetching ? ( | |
<p>Fetching todos...</p> | |
) : ( | |
<ul> | |
{this.state.todos.map(todo => ( | |
<li key={todo.id}> | |
{todo.title}{" "} | |
<button onClick={e => this.handleDelete(todo.id)}>x</button> | |
</li> | |
))} | |
</ul> | |
)} | |
</div> | |
); | |
} | |
} | |
export default App; |
npm install --save axios
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://testreacttodoapp.herokuapp.com/todos