- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试了解重新选择方法 createStructuredSelector
在 Typescript 中的工作原理。我经常看到这种模式:
export interface SomeProps {
readonly property1: string;
readonly property2: boolean;
readonly property3: number;
}
export interface SomeOwnProps {
readonly property3: number;
}
export const someSelector = createStructuredSelector<
SomeState,
SomeOwnProps,
SomeProps
>({
property1: property1Selector,
property2: property2Selector,
property3: (_, props) => props.property3
});
我不明白尖括号内类型的用途。我认为SomeState
是Redux store state,SomeOwnProps
是父组件传入的props,SomeProps
是所有的props该组件使用。但是 SomeOwnProps
和 SomeProps
有什么区别,为什么需要两者?为什么不能只使用 SomeProps
因为它还包含 SomeOwnProps
中定义的属性?谢谢!
最佳答案
createStructuredSelector
有两种变体 — 一种采用组件自己的 props,另一种则不采用。如果您想要的选择不依赖于 Prop ,那么选择后者会更容易。
考虑这个稍微做作的例子:
/**
* Data source (Redux store or a single reducer).
*/
interface State {
readonly property1: string;
readonly property2: boolean;
readonly property3: number;
}
/**
* Your component's OwnProps.
*/
interface OwnProps {
index: 1 | 2 | 3;
}
/**
* The interface you want to create.
*/
interface DesiredSelection {
someString: string;
someNumber: number;
};
当您的选择不取决于 Prop 时:
const mySelector = createStructuredSelector<State, DesiredSelection>({
someString: state => state.property1,
someNumber: state => state.property3
});
当您的选择确实取决于 Prop 时:
const mySelectorBasedOnProps = createStructuredSelector<State, OwnProps, DesiredSelection>({
someString: state => state.property1,
someNumber: (state, props) =>
(props.index === 1)
? 1
: state.property3
});
需要区分OwnProps
和DesiredSelection
,因为前者会影响后者。根据您的组件从其父级收到的 Prop ,从 Redux 商店的不同部分选择数据是一种常见的模式。
比较现实的例子:
interface State {
translations: {
au: 'Hello, mate!',
uk: 'Welcome, old chap!'
}
}
interface OwnProps {
country: 'au' | 'uk';
}
interface ConnectedProps {
message: string
};
/**
* These are all props your component will receive.
*/
type Props = OwnProps & ConnectedProps;
const getMessage = createStructuredSelector<State, OwnProps, ConnectedProps>({
message: (state, props) => state.translations[props.country]
});
关于reactjs - reselect createStructuredSelector 如何在 Typescript 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53963645/
当我将更多选择器组合在一起时,我发现我正在重新排序选择器的定义位置。例如, 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
我是一名优秀的程序员,十分优秀!