Redux x类型的结果函数=>;x是不必要的吗?

Redux x类型的结果函数=>;x是不必要的吗?,redux,reselect,Redux,Reselect,我对重新选择的工作原理的理解可能存在差距 如果我正确理解以下代码: const getTemplates = (state) => state.templates.templates; export const getTemplatesSelector = createSelector( [getTemplates], templates => templates ); 在不损失任何东西的情况下,也可以写为: export const getTemplatesSelect

我对重新选择的工作原理的理解可能存在差距

如果我正确理解以下代码:

const getTemplates = (state) => state.templates.templates;

export const getTemplatesSelector = createSelector(
  [getTemplates],
  templates => templates
);
在不损失任何东西的情况下,也可以写为:

export const getTemplatesSelector = (state) => state.templates.templates;
如果我理解正确的话,原因是Reselect检查它的第一个参数,以及它是否接收到与之前相同的对象,并返回缓存输出。它不检查值是否相等

当getTemplates返回一个新对象时,即当state.templates.templates引用一个新对象时,重新选择将只运行
templates=>templates
。 这种情况下的输入将与输入完全相同,因此使用重新选择不会获得缓存功能

只有当函数(在本例中为
templates=>templates
)本身返回一个新对象时,才能通过重新选择的缓存功能获得性能,例如通过.filter或.map或类似的方式。在这种情况下,虽然返回的对象是相同的,但不会进行任何更改,因此,通过重新选择的记忆,我们将一无所获

我写的东西有什么问题吗? 我主要是想确保我正确理解重选的工作原理

--编辑--


我忘了提到,我写的东西假设对象
状态为.templates.templates
是不可变的,没有突变。

是的,在您的情况下,
重新选择
不会带来任何好处,因为每次调用都会对
getTemplates
进行评估

2个最重要的场景
reselect
如下:

  • 结果函数返回新对象(如您所述)时,稳定选择器的输出
  • 结果函数的计算成本很高时,改进选择器的性能