Advertisement
Guest User

Untitled

a guest
Nov 15th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import _ from 'lodash';
  2. import React , { Component } from 'react';
  3. import { Link } from 'react-router-dom'
  4. import { connect } from 'react-redux';
  5. import { fetchPosts} from "../actions/index"
  6.  
  7.  
  8. class PostIndex extends Component {
  9.  
  10.     componentDidMount() {
  11.         this.props.fetchPosts();
  12.     }
  13.  
  14.     renderPosts() {
  15.         return _.map(this.props.posts, post => {
  16.             if (post.title == null) return;
  17.             return (
  18.               <li className="list-group-item" key={post.id} >
  19.                   {post.title}
  20.               </li>
  21.             );
  22.         });
  23.     }
  24.  
  25.  
  26.     render() {
  27.         console.log(this.props.posts);
  28.         return (
  29.             <div>
  30.                 <div className="text-xs-right">
  31.                     <Link className="btn btn-primary" to="/posts/new" >
  32.                         Add a Post
  33.                     </Link>
  34.                 </div>
  35.                 <h3>Posts</h3>
  36.                 <ul className="list-group" >
  37.                     { this.renderPosts() }
  38.                 </ul>
  39.             </div>
  40.         );
  41.     }
  42. }
  43.  
  44. const mapStateToProps = (state) => {
  45.     return {
  46.         posts: state.posts
  47.     }
  48. };
  49.  
  50.  
  51.  
  52. export default connect(mapStateToProps, { fetchPosts })(PostIndex);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement