- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下选择器:
const getAllAddresses = (withStartEnd) =>
createSelector(
[getAllAddressesSelector, getStartAddressSelector, getEndAddressSelector],
(all, startAddress, endAddress) => {
if (!withStartEnd) return [...Object.values(all)];
return [startAddress, ...Object.values(all), endAddress];
}
);
我注意到选择器每次都在重新计算,当 all
、startAddress
和 endAddress
没有改变时发生。如果我删除选择器函数的输入,变成这样:
const getAllAddresses = (
createSelector(
[getAllAddressesSelector, getStartAddressSelector, getEndAddressSelector],
(all, startAddress, endAddress) => {
return [startAddress, ...Object.values(all), endAddress];
}
)
);
然后一切都按预期工作,并且选择器不会在每次调用时重新计算。好像我在选择器概念中遗漏了一些东西。任何帮助将不胜感激。
最佳答案
更新:
请引用How do I create a selector that takes an argument?
简而言之:只有当您传递静态参数并在 mapStateToProps
之外创建工厂函数时,您的操作方式才会起作用。对于动态参数,它更复杂,请遵循我在上面提到的资源。
每次调用 mapStateToProps
时都会重新计算您的选择器的原因是调用 getAllAddresses
会创建一个新的 createSelector
实例,并且内存会成功'工作。
原答案:
简而言之,reselect
根据身份检查 ===
确定输入选择器更改。
因此,如果您的输入选择器总是创建并返回一个新的对象或数组,那么您的选择器每次都会重新计算。
为了解决重新计算问题:
来自 reselect
文档:
Why is my selector recomputing when the input state stays the same? (请点击链接,有很好的例子)
Check that your memoization function is compatible with your state update function (i.e. the reducer if you are using Redux). For example, a selector created with createSelector that recomputes unexpectedly may be receiving a new object on each update whether the values it contains have changed or not. createSelector uses an identity check (===) to detect that an input has changed, so returning a new object on each update means that the selector will recompute on each update.
关于javascript - Redux Reselect - 带参数输入的选择器正在重新计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56312122/
当我将更多选择器组合在一起时,我发现我正在重新排序选择器的定义位置。例如, export const selectNav = state => state.nav; export const sele
我正在尝试将动态参数传递给重新选择选择器。这样做的原因是这个参数实际上是一个事先不知道的角度路线参数。它也不能是国家的一部分。 以下是来自订阅组件的传递路由参数的相关代码: this.store.se
由于一些奇怪的奇怪原因,我的重新选择选择器返回了这个函数: ƒ () { if (!areArgumentsShallowlyEqual(equalityCheck, lastArgs, ar
我有一个大型选择器,它遍历数组并为数组中的每个项目调用一个选择器。有什么简单的方法可以解决这个问题吗? 看起来像这样: const memoizedGetPatientSymptomSeries =
我正在尝试集成reselect进入我当前的应用程序,一如既往,首先我开始阅读文档,然后如果需要,再阅读另一个资源。我无法理解文档的一个特殊部分,也找不到可以更清晰地解释的资源。现在我来这里是为了得到一
我有以下选择器: const getAllAddresses = (withStartEnd) => createSelector( [getAllAddressesSelec
假设我有一个具有这种状态结构的 redux store: { items: { "id1" : { foo: "foo1", bar: "bar1" },
编辑:添加了 package.json 摘录 我正在尝试在现有的 React 项目中实现 typescript ,但我遇到了 Reselect 的困难图书馆。 Typescript 编译器坚持导入 c
将 toJS 与重新选择库一起使用时,我在 React 应用程序的选择器中遇到错误。我尝试导入 toJS 以及不导入它,但无论如何我都会收到错误。 nodeCreationWindow.get(...
我正在尝试过滤和显示“postID”与当前帖子 ID 匹配的评论。我正在使用 Redux/Reselect,它可以工作,但有时会返回一个错误,提示 post._id is undefined/null
我尝试为可编辑的 html 元素构建工具栏。我有带有“粗体”、“斜体”等的工具栏。 当用户选择任何文本并单击“粗体”时,我检查是否存在跨度,如果不存在则插入。 let hasWrap = (selec
我最近才遇到过重新选择这样对我来说很新的包。我仔细阅读了官方文档,并且已经对我的第一个 selectors 有了经验。但只有一件事我无法理解 - 在组件中实现 selectors 以创建正确的内存选择
我在我的应用程序中使用了 React + Redux + Reselect + Immutable.js 的终极组合。我喜欢重新选择的想法,因为它让我保持状态(由 reducer 维护)尽可能简单。我
据我所知,用于重新选择的 .d.ts 文件 ( https://github.com/reactjs/reselect ) 是正确的。那么这是怎么回事……是 Typescript 编译器的问题吗?我的
我创建了一个根据用户输入过滤数据的应用程序。 // ui component import React from 'react'; import { useDispatch, useSelector
我已经坚持了一段时间。 这是我的 redux-state 的一部分 state: { teachers: [ { teacherId: 'ttt1', teacherName: 'Susa
我正在尝试了解重新选择方法 createStructuredSelector 在 Typescript 中的工作原理。我经常看到这种模式: export interface SomeProps {
我的应用程序已经有大量供各种容器对象使用的选择器。这些非常适合访问状态的不同部分,并使重构状态变得更加容易。 现在我想在我的一些 reducer 函数中使用我的选择器。问题是在 reducer 内部,
我想使用基于 mapStateToProps 的某些 ownProps 的重新选择来创建一个具有内存功能的选择器。 最佳答案 您可以通过使用 react-redux 提供的 connect 方法将选择
我正在实现 React + Redux 应用程序,但发现将 Reselect 集成到我的应用程序中很困难; 下面是我的代码 store.js import "regenerator-runtime
我是一名优秀的程序员,十分优秀!