反应redux-将数据向下传递多个级别

时间:2018-07-26 07:02:58

标签: reactjs redux

这是redux的新手,在网上找不到我想要实现的任何类似示例,也许我对redux感到困惑。说我有类似的东西我已经简化了

const mapStateToProps = state => {
  return { articles: state.articles };
};

const ConnectedList = ({ articles }) => (
  <ul className="list-group list-group-flush">
    <ConnectedListItems {...articles}/>
  </ul>
);

const ConnectedListItems = ({articles}) =>{
  // maybe render here or pass articles to another component
}

const List = connect(mapStateToProps)(ConnectedList);
export default List;

我是否可以通过某种方式直接传递给ConnectedListItems,我感觉自己正在钻取atm。 ConnectedList不需要文章,ConnectedList也可以是一个类

谢谢

1 个答案:

答案 0 :(得分:0)

您可以像使用 ConnectedList 组件一样mapStateToProps使用 ConnectedListItems 组件。

需要更改:-

let ConnectedListItems = ({articles}) =>{
  // maybe render here or pass articles to another component
}
let ConnectedListItems = connect(mapStateToProps)(ConnectedListItems);

更新代码:-

const mapStateToProps = state => {
  return { articles: state.articles };
};

const ConnectedList = ({ articles }) => (
  <ul className="list-group list-group-flush">
    <ConnectedListItems />
  </ul>
);

let ConnectedListItems = ({articles}) =>{
  // maybe render here or pass articles to another component
}

ConnectedListItems = connect(mapStateToProps)(ConnectedListItems);

const List = connect(mapStateToProps)(ConnectedList);
export default List;