spermspace

Untitled

Apr 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import Todo from '../App';
  3.  
  4. class ListTodo extends Component {
  5.  
  6.   constructor(props){
  7.     super(props);
  8.     this.state = {
  9.       todos: [],
  10.       rendView: false,
  11.       isOpen: props.defaultOpen
  12.     }
  13.  
  14.   }
  15.  
  16.   async componentDidMount() {
  17.   try {
  18.     const res = await fetch('http://127.0.0.1:8000/api/');
  19.     const todos = await res.json();
  20.     this.setState({
  21.       todos
  22.     });
  23.   } catch (e) {
  24.     console.log(e);
  25.   }
  26. }
  27.  
  28.   render(){
  29.     return (
  30.       <div className="container">
  31.         {this.state.todos.map(i => (
  32.  
  33.           <div className="items" key={i.id}>
  34.             <h3>{i.title}</h3>
  35.             {this.state.isOpen && <span className="text">
  36.               {i.description}
  37.             </span>}
  38.             <button className="btn m-1" onClick={this.clickClose}>
  39.               {this.state.isOpen ? 'Скрыть' : 'Показать'}
  40.             </button>
  41.           </div>
  42.  
  43.         ))}
  44.       </div>
  45.     )
  46.   }
  47.  
  48.   clickClose = () => {
  49.       this.setState({
  50.         isOpen: !this.state.isOpen
  51.       })
  52.   }
  53.  
  54. }
  55.  
  56. export default ListTodo;
Add Comment
Please, Sign In to add comment