如何在Immutablejs和Redux以及Flux和React中设置Ember类似计算属性

egu*_*eys 9 flux reactjs immutable.js redux

我习惯于在Ember对象模型中计算属性.这是指定依赖于其他属性的计算属性的便捷方式.

fullName取决于firstNamelastName,我可以将计算属性设置为函数,computeProperties并在computeProperties每次进行更改时调用.

例:

function computeFullName(state) {
  const fullName = state.get('firstName') + state.get('lastName');
  const nextState = state.set('fullName', fullName);
  return nextState;
}

function computeProperties(state) {
  const nextState = computeFullName(state);
  return nextState;
}

// store action handler
[handleActionX](state) {

  let nextState = state.set('firstName', 'John');
  nextState = state.set('lastName', 'Doe');

  nextState = computeProperties(nextState);

  return nextState;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法自动设置计算属性,以便我不必每次都调用额外的函数.在Redux或ImmutableJS中.

Dan*_*mov 11

Redux的作者在这里!

使用WildService建议的重新选择是要走的路.我认为我们不会将其纳入核心,因为重新选择可以很好地完成工作,我们可以将它作为一个单独的库.

我想注意几件事:

  • 即使重新选择,您也不希望在reducer中计算数据.选择器应该 reducers管理的状态下运行.换句话说,选择器是Redux存储状态和组件之间的一步 - 它们不在Reducer中.您必须将Redux状态标准化,以便更新.

  • 我们实际上鼓励您在相关减速器旁边定义选择器,这样当您更改状态形状时,您不必更改组件 - 他们将改为使用选择器.您可以在Flux ComparisonRedux文件夹中看到此示例

  • 我们有一个文档页面介绍了重新选择并描述了如何使用它来计算派生数据.看看这个.