reactjs-redux,是否可以有一个由2个组件使用的状态

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

本文关键字:组件 状态 2个 是否 有一个 reactjs-redux      更新时间:2023-09-26

我有这个错误:

bundle.js:2586警告:失败的道具类型:为DisplayTrendings提供的类型为array的道具posts无效,应为object。检查Connect(DisplayTrendings)的渲染方法。

我有两个列表,显示ajax调用中的项目。

这是我的集装箱:

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

这是我的两个组成部分:

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

只有第二个列表DisplayWikiList显示列表。另一个是空白的,它没有通过它

所以我想知道,是因为我在两个组件中使用了相同的道具吗?

当我查看退货帖子时,我的集装箱是:

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

如果我显示我发送给我的组件的内容:

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

我到底做错了什么

React支持给定相同道具的多种数据类型;所以你不必把自己局限于其中一个:

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

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

编辑你所做的是可行的,这意味着你绝对能够毫无问题地将同一道具传递给多个子组件。也就是说,看起来您的两个子组件都在对传入的道具数据使用.map()函数,但.map()只能由数组使用,而不能由对象使用。看起来broadmatch是一个对象,这意味着map()迭代器将无法工作。尝试使用lodash或下划线中的_.map()

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