在React Container中没有呈现GET响应

时间:2018-02-09 20:57:35

标签: reactjs rendering

以下是我的容器代码。我正在从WordPress博客中提取帖子。

我在控制台中获得了正确的控制台日志,但由于某些原因,容器中的这部分{this.props.posts.map(this.renderPosts)}未正确呈现,它只呈现空白P元素。

我怎样才能找出问题?

import React, {Component} from 'react';
import {Link} from 'react-router-dom';
import {connect} from 'react-redux';
import {fetchPosts} from '../../action/index';
import {bindActionCreators} from 'redux';

class Blog extends Component {

    componentDidMount(){
        this.props.fetchPosts();
    }
   renderPosts(postData){               
      return Object.keys(postData.posts).map(id => {
        return (
                <div id={postData.posts[id].site_ID}>{postData.posts[id].title}</div>
        ); 
    });
}
    render(){
        console.log('Posts are: ',this.props.posts);
        return (
            <div>
                <Link to="/posts/new"><button> Add a New Post </button> </Link>  
                <p>
                    {this.props.posts.map(this.renderPosts)}
                </p>  
            </div>
        );
    }
}
function mapStateToProps(state){
    return {
        posts: state.posts
    };
}
function mapDispatchToProps(dispatch){
    return bindActionCreators({fetchPosts}, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(Blog);

1 个答案:

答案 0 :(得分:2)

renderPosts(postData){               
   return Object.keys(postData.posts).map(id => {
        console.log('pd', postData.posts[id]);
        return (
            <tr>
                <p>{postData.posts[id].title}</p>
            </tr>
        ); 
    });
}

你错过了renderPosts()

的回报