简体   繁体   English

this.props中的数组(由redux提供)返回undefined

[英]Array in this.props (provided by redux) returns undefined

I fetch data via Redux in my component like so: 我通过Redux在组件中获取数据,如下所示:

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);

This code is working and will put out the following JSON in the console: JSON output 此代码有效,并将在控制台中输出以下JSON: JSON输出

When trying to log this.props or this.props.posts everything returns just fine. 尝试记录this.propsthis.props.posts一切都很好。 But when trying to log this.props.posts.allposts the console returns undefined. 但是,当尝试记录this.props.posts.allposts ,控制台将返回undefined。

If it returns json with multiple values then you need to do it as 如果它返回具有多个值的json,则需要按以下步骤进行操作

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


})
}
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM