2017-08-08 52 views
0

我在組件通過終極版中獲取數據,像這樣:陣列this.props(由終極版提供)返回undefined

import React, {Component} from 'react'; 
import {bindActionCreators} from 'redux'; 
import {connect} from 'react-redux'; 
import * as actionCreators from './../../_actions/actionCreators'; 

import './Posts.css'; 

function mapStateToProps(state) { 
    return {posts: state.posts, comments: state.comments} 
} 

function mapDispatchToProps(dispatch) { 
    return bindActionCreators(actionCreators, dispatch); 
} 

class Posts extends Component { 
    render() { 
    return (
     <div> 
     <h1>POSTS</h1> 

     { 
      console.log(this.props) 
     } 


     </div> 
    ) 
    } 
} 

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

此代碼工作,將推出以下JSON在控制檯:JSON output

當試圖登錄this.propsthis.props.posts時,一切都會恢復正常。 但是,當試圖登錄this.props.posts.allposts控制檯返回未定義。

回答

0

如果返回JSON有多個值,那麼你需要做的是爲

return(
<div> 
{ 
this.props.posts.allposts(function(allposts,i) 
{ 
return <h1 key={i}>{allposts}</h1> 


}) 
} 
</div>