我正在开发一个 react (0.14) + redux (3.3) + react-redux (4.4) + react-router (2.0) 应用程序,它包含三个PostsLists组件:包装这些组件:PostBlog
┌──────────────────────────────────────────────┐
│ │
│ ┌─────────────┐┌──────────────────────────┐ │
│ │ ││ │ │
│ │ Post 1 ││ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │
│ │ Post 2 ││ Post title │ │
│ │ Post 3 ││ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │
│ │ ││ │ │
│ │ ││ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │
│ │ ││ │ │
│ │ ││ │ │ │ │
│ │ ││ Post contents │ │
│ │ ││ │ │ │ │
│ │ ││ │ │
│ │ ││ │ │ │ │
│ │ ││ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ │ │
│ │ PostsList ││ Post │ │
│ └─────────────┘└──────────────────────────┘ │
│ │
│ Blog │
└──────────────────────────────────────────────┘
到目前为止,我的 redux 状态只是一系列博客文章:
{posts: [{title: 'Post 1', contents: '…'}]}
路线是
/posts
/posts/:id
/posts仅显示at PostLists,同时显示 on /posts/:id Post(它将是 的子级Blog)。帖子列表Links to /posts/:id。
Blog将在挂载时从 HTTP api 获取博客文章,因此如果加载,例如,/posts/1 Post可能会在有要显示的文章之前挂载。
我的问题是关于管理当前的活动帖子。我最初的想法——我仍然很喜欢——是current_post在该州拥有一个字段:
{posts: [{title: 'Post 1', contents: '…'}],
current_post: {title: 'Post 1', contents: '…'}}
但是,例如在加载时/posts/1,谁应该负责更新current_post(以确保Post在更改时重新加载的方式)?如果Post是一个容器并且我mapStateToProps曾经拥有过,它会在更改current_post时重新渲染吗?current_post
另一种选择是检查props.params.id然后Post找到正确的帖子,但这似乎有点脏。人们通常如何构建这样的应用程序?