Guest User

Untitled

a guest
Feb 13th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4.  
  5. const API = 'http://127.0.0.1:8000/app/';
  6. class Blog extends React.Component{
  7.  
  8.     constructor(props){
  9.         super(props);
  10.  
  11.         this.state = {
  12.             items: []
  13.         }
  14.     }
  15.     async componentDidMount() {
  16.         try {
  17.           const res = await fetch(API);
  18.           const items = await res.json();
  19.           this.setState({
  20.             items
  21.           });
  22.         } catch (e) {
  23.           console.log(e);
  24.         }
  25.       }
  26.  
  27.  
  28.       render() {
  29.        
  30.         return (
  31.             <div>
  32.               {this.state.items.map(item => (
  33.                 <div key={item.id}>
  34.                   <h1>{item.title}</h1>
  35.                   <span>{item.content}</span>
  36.                 </div>
  37.               ))}
  38.             </div>
  39.           );
  40.         }
  41.     }
  42.    
  43.  
  44.  
  45. ReactDOM.render(
  46.     <Blog />,
  47.     document.getElementById('root')
  48. );
Advertisement
Add Comment
Please, Sign In to add comment