robertvari

PostList axios exmample

Apr 12th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, {useState, useEffect} from 'react';
  2. import axios from 'axios';
  3. import Post from "./Post"
  4. import './PostList_style.css'
  5.  
  6. function PostList(props) {
  7.     const [post_list, set_post_list] = useState([]);
  8.  
  9.     const get_posts = () =>{
  10.         axios.get('http://127.0.0.1:8000/api/posts/')
  11.             .then(res => set_post_list(res.data))
  12.             .catch(err => console.log(err))
  13.     };
  14.  
  15.     useEffect(()=>{
  16.         get_posts()
  17.     }, []);
  18.  
  19.     return (
  20.         <div className="post_list_layout">
  21.             {post_list.map(post_data => <Post key={post_data.id} data={post_data}/>)}
  22.         </div>
  23.     );
  24. }
  25.  
  26. export default PostList;
Add Comment
Please, Sign In to add comment