23

我在思考如何实现一个reducer,它的实体可以有相同类型的孩子时有点卡住了。

让我们以reddit评论为例:每个评论可以有子评论,可以有自己的评论等。为了简单起见,评论是类型的记录{id, pageId, value, children}pageIdreddit页面。

一个人将如何围绕它来建模减速器?我正在考虑让reducer成为一个地图->评论的ID,您可以在其中使用pageId.

问题在于,例如,当我们想向嵌套的评论添加评论时:我们需要在地图的根目录上创建记录,然后将其 id 添加到父子属性中。要显示我们需要获取所有评论的所有评论,过滤我们在顶部拥有的评论(例如,将作为有序列表保存在页面缩减器中)然后迭代它们,从评论对象中获取我们遇到使用递归的孩子。

有比这更好的方法还是有缺陷?

4

2 回答 2

40

对此的官方解决方案是使用normalizr保持您的状态如下:

{
  comments: {
    1: {
      id: 1,
      children: [2, 3]
    },
    2: {
      id: 2,
      children: []
    },
    3: {
      id: 3,
      children: [42]
    },
    ...
  }
}

connect()你是对的,你需要Comment组件,这样每个组件都可以从 Redux 商店递归查询children它感兴趣的内容:

class Comment extends Component {
  static propTypes = {
    comment: PropTypes.object.isRequired,
    childComments: PropTypes.arrayOf(PropTypes.object.isRequired).isRequired
  },

  render() {
    return (
      <div>
        {this.props.comment.text}
        {this.props.childComments.map(child => <Comment key={child.id} comment={child} />)}
      </div> 
    );
  }
}

function mapStateToProps(state, ownProps) {
  return {
    childComments: ownProps.comment.children.map(id => state.comments[id])
  };
}

Comment = connect(mapStateToProps)(Comment);
export default Comment;

我们认为这是一个很好的妥协。comment您作为道具传递,但组件childrenComments从商店中检索。

于 2015-10-03T13:41:35.860 回答
1

您的商店(reducer)结构可能与您想要的视图模型(作为道具传递给组件的视图模型)不同。您可以将所有评论保存在数组中,并通过高级“智能”组件上的 mapStateToProps 中的链接将它们映射到树。您将在 reducer 中获得简单的状态管理,并为组件提供方便的视图模型。

于 2015-10-03T13:29:12.283 回答