React + Flux 架构下维护 List 中的 focused Item

Maintain focused Item in List under React + Flux architecture

我有一个项目列表,还有一个 div 用于显示项目的详细信息。例如,

--------------------------------------------
| Items List  |  Selected Item Detail      |
--------------------------------------------
| Item A      |   Item C is selected       |
| Item B      |   Item C is a cool         |
| Item C*     |                            |
| Item D      |                            |
--------------------------------------------

我的组件是:

- AppContainer
  - Item List
    - Item
  - Selected Item Detail

AppContainer 具有以下统计信息

我的问题来了:实现是否正确?

  1. 是否应该在AppContainer中维护selectedItemId(一个整数),并在将selected item传递给Selected Item Detail时通过id找到对应的item?
  2. 或者我应该保留一个 selectedItem(一个对象)作为状态,并将其直接传递给 Selected Item Detail
  3. 我应该在 Flux 的商店中保留 selectedItem 还是 selectedItemId? (假定我在我的应用程序中使用 Flux 后将 allItems 保留在商店中。)

谢谢。

正如您的问题所暗示的那样,有多种方法可以实现同一件事,所以我认为 实施是否正确 无关哪个实现最有意义

Should I maintain selectedItemId (an integer) in AppContainer, and find the corresponding item by the id when passing selected item to Selected Item Detail? Or should I keep a selectedItem (an object) as state, and pass it to Selected Item Detail directly?

我认为一般来说,最好保持状态尽可能紧凑,并派生 依赖于实际需要的其他数据的数据。还有“不要重复自己”的旧 DRY 标准。由于所选项目可以从所有项目的集合和索引中导出,因此我将存储索引而不是项目的额外副本。

Should I keep the selectedItem or selectedItemId in Flux's Store? (given I keep the allItems in Store after using Flux in my application.)

出于与上述相同的原因,我会存储 ID 而不是项目的副本。