App.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import React from "react";
  2. import axios from "axios";
  3. import "./App.scss";
  4. import AddTodo from "./components/AddTodo";
  5. import TodoList from "./components/TodoList";
  6. export default class App extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. todos: [],
  11. };
  12. }
  13. componentDidMount() {
  14. axios
  15. .get("/api")
  16. .then((response) => {
  17. this.setState({
  18. todos: response.data.data,
  19. });
  20. })
  21. .catch((e) => console.log("Error : ", e));
  22. }
  23. handleAddTodo = (value) => {
  24. axios
  25. .post("/api/todos", { text: value })
  26. .then(() => {
  27. this.setState({
  28. todos: [...this.state.todos, { text: value }],
  29. });
  30. })
  31. .catch((e) => console.log("Error : ", e));
  32. };
  33. render() {
  34. return (
  35. <div className="App container">
  36. <div className="container-fluid">
  37. <div className="row">
  38. <div className="col-xs-12 col-sm-8 col-md-8 offset-md-2">
  39. <h1>Todos</h1>
  40. <div className="todo-app">
  41. <AddTodo handleAddTodo={this.handleAddTodo} />
  42. <TodoList todos={this.state.todos} />
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. );
  49. }
  50. }