简体   繁体   English

reactjs redux,是否可以通过2个组件进行状态使用?

[英]reactjs redux, is it possible to have a state use by 2 components?

I am having this error: 我有这个错误:

bundle.js:2586 Warning: Failed propType: Invalid prop posts of type array supplied to DisplayTrendings , expected object . bundle.js:2586警告:propType失败:提供给DisplayTrendings预期object的类型array无效道具posts Check the render method of Connect(DisplayTrendings) . 检查Connect(DisplayTrendings)的render方法。

And I have 2 lists that display items from an ajax call. 我有2个列表显示ajax调用中的项目。

Here is my container: 这是我的容器:

export default class DisplayTrendings extends Component {
  constructor(props) {
    super(props)
  }

  componentDidMount() {
    const { dispatch } = this.props;
    dispatch(fetchPosts('trendings'));
  }

  render() {
    const { posts, isFetching } = this.props;
    console.log("posts in container :", posts['wikipedia']);
    const isEmpty = posts.length === 0;
    return (
      <Card className="card-main">
          <h2 className="trending-title">Trendings</h2>
          <ColumnName />

          {isEmpty
            ? (isFetching ? <h2>Loading...</h2> : <h2>Empty request.</h2>)
            : <div className="row">
                <div className="col-md-6">
                  <DisplayWikiList style={{ opacity: isFetching ? 0.5 : 1 }} postsWiki={posts['wikipedia']} />
                </div>
                <div className="col-md-6">
                  <DisplayBroadmatchList style={{ opacity: isFetching ? 0.5 : 1 }} postsBroad={posts['broadmatch']} />
                </div>
              </div>
          }

          <div className="col-md-6">
            <div className="row">
              <div className="col-xs-6">
                <input type="text" className="bespoke-label" />
              </div>
              <DisplayOutPut />
            </div>
          </div>
      </Card>
    );
  }
};

DisplayTrendings.propTypes = {
  posts: PropTypes.object.isRequired,
  isFetching: PropTypes.bool.isRequired,
  dispatch: PropTypes.func
};

function mapStateToProps(state) {
  console.log("state: ", state);
  const {
    isFetching,
    items: posts
  } = {
    isFetching: state.posts.isFetching,
    items: state.posts.items
  }
  return {
    posts,
    isFetching
  };
};

And here are my 2 components: 这是我的两个组成部分:

export default class DisplayBroadmatchList extends Component {
  render() {
    if (this.props.postsBroad) {
    return (
      <div className="row">
        <div className="col-md-6">
          <ul>
            {this.props.postsBroad.map((post, i) =>
              <li key={i}>{ post.MarkerName }</li>
            )}
          </ul>
        </div>
      </div>
    );
    } else {
      return (
        <div></div>
      );
    }
  }
};
DisplayBroadmatchList.propTypes = {
  postsBroad: PropTypes.object
};

export default class DisplayWikiList extends Component {
  render() {
    if (this.props.postsWiki) {
    return (
      <div className="row">
        <div className="col-md-6">
          <ul>
            <li key="bui">yolo</li>
            {this.props.postsWiki.map((post, i) =>
              <li key={i}>{ post.Entity }</li>
            )}
          </ul>
        </div>
      </div>
    );      
    } else {
      return (
        <div></div>
      );
    }
  }
};
DisplayWikiList.propTypes = {
  postsWiki: PropTypes.object
};

Only the second list, DisplayWikiList is displaying a list. 只有第二个列表,DisplayWikiList才显示列表。 The other one is blank and it doesn't go through it! 另一个是空白的,它没有通过它!

So I am wondering, is it because I am using the same props into the 2 components ? 所以我想知道, 是因为我在2个组件中使用相同的道具

When I check the returns posts, to my container it's : 当我检查退货帖子时,我的容器是:

posts[ ->broadmatch {object1{name, id}, object2{name, id}...} ->wikipedia [object1{name, id}, object2{name, id}...] ]

and if I display what I send to my component: 如果我显示我发送给我的组件的内容:

posts['broadmatch'] = {object1{name, id}, object2{name, id}...} posts ['broadmatch'] = {object1 {name,id},object2 {name,id} ...}

What am I really doing wrong here? 我在这里做错了什么?

React supports multiple data types given the same prop; React支持给定相同prop的多种数据类型; so you don't have to limit yourself to one or the other: 所以你不必将自己限制在一个或另一个:

DisplayWikiList.propTypes = {
    postsWiki: React.PropTypes.oneOfType([
         React.PropTypes.object,
         React.PropTypes.array
    ]),
};

Via https://facebook.github.io/react/docs/reusable-components.html 通过https://facebook.github.io/react/docs/reusable-components.html

EDIT What you're doing is viable, meaning you are absolutely able to pass the same prop to multiple sub-components without issue. 编辑您所做的是可行的,这意味着您绝对能够将相同的道具传递给多个子组件而不会出现问题。 That said, it looks as though both your subcomponents are using the .map() function on the incoming prop data, but .map() can only be used by arrays, not objects. 也就是说,看起来你的子组件都在传入的prop数据上使用.map()函数,但.map()只能由数组使用,而不能由对象使用。 And it looks like broadmatch is an object, meaning the map() iterator will not work. 看起来broadmatch是一个对象,这意味着map()迭代器不起作用。 Try using _.map() from lodash or underscore . 尝试使用_.map()lodash下划线

{  
    _.map(this.props.postsWiki, (post, key) =>
        <li key={key}>{ post.Entity }</li>
    )
}

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

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