- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试向旧组件添加类型注释,但不确定是否 connect
的类型支持我正在尝试做的事情。该组件是一个连接的组件,它接受一个广泛类型的 prop,然后使用该 prop 查找状态中的值,并吐出一个更窄的 prop。
目前,我可以用广泛的类型定义 Prop ,代码就可以工作了。但是,我在组件中编写的任何代码都必须符合这种广泛的类型,即使我知道它已经缩小了。因此,为了满足 typescript ,我要么必须添加类型断言,要么必须进行不必要的类型检查。
下面是一个简化的代码示例,用于显示该行为:
interface ExampleProps {
thing: string | number;
}
const Example extends React.Component<ExampleProps> {
render() {
const problem = this.props.thing * 2; // type error
// Workaround, but losing some typesafety
// const notAProblem = (this.props.thing as number) * 2;
// Workaround, but doing unnecessary checks
// if (typeof this.props.thing === 'number') {
// const notAProblem = this.props.thing * 2;
// }
return null;
}
}
const mapStateToProps = (state: RootState, otherProps: ExampleProps) => {
if (typeof otherProps.thing === 'string') {
return {
thing: state.lookup[otherProps.thing]; //resolves to a number
}
}
return {
thing, // also a number
}
}
export default connect(mapStateToProps)(Example)
在上面的代码中,typescript 正确地指出了 const problem: number = this.props.thing * 2;
是个问题。使用我定义的类型,this.props.thing
可能是一个字符串,所以它会正确给出错误 The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.
是否可以使用 connect
以不同的 Prop 进来而不是出去的方式?换句话说,我需要有人能够渲染 <Example thing={2} />
或 <Example thing={"2"} />
,但要让示例中的代码知道这一点,多亏了 mapStateToProps,thing
只能是 number
最佳答案
一种方法是使“事物”成为 ExampleProps
上的通用类型,即:
interface ExampleProps<T extends (string | number)> {
thing: T;
}
然后你可以分别在类和函数类型签名中指定不同的具体类型:
class Example extends Component<ExampleProps<number>> {
render() {
const problem: number = this.props.thing * 2;
return null;
}
}
const mapRootStateToExampleProps = (state: RootState, {thing}: ExampleProps<string | number>) => {
if (typeof thing === 'string') {
return {
thing: state.lookup[thing] //resolves to a number
}
}
return {
thing, // also a number
}
}
不过,这可能不是最佳解决方案,因为您的类型签名可能会很快变得冗长。它只对 ExampleProps 接口(interface)的一个或两个字段真正有意义。
另一种方法是声明一个表示传入 Prop 的基本接口(interface),然后声明一个扩展它的接口(interface)表示传出的 Prop - 然后您只需要指定在“传出”时修改的类型,这样您没有重复很多代码:
interface ExamplePropsIn {
thing: string | number;
other: string;
}
interface ExamplePropsOut extends ExamplePropsIn {
thing; number;
}
class Example extends Component<ExamplePropsOut> {
render() {
const problem: number = this.props.thing * 2;
return null;
}
}
const mapRootStateToExampleProps = (state: RootState, {thing}: ExamplePropsIn) => {
if (typeof thing === 'string') {
return {
thing: state.lookup[thing] //resolves to a number
}
}
return {
thing, // also a number
}
}
关于reactjs - mapStateToProps 可以缩小 props 的类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58241408/
我仍在学习如何将 API 数据与 react 和 nextjs 一起使用。但是,为什么我的函数只在我编写 {props.props.title} 而不是我期望的 {props.title} 时起作用?
我仍在学习如何将 API 数据与 react 和 nextjs 一起使用。但是,为什么我的函数只在我编写 {props.props.title} 而不是我期望的 {props.title} 时起作用?
我正在用 TypeScript 构建一个 React 应用程序。我有一个 RequiresPermission基于谓词的组件应该渲染一个或另一个组件并转发所有 Prop 。 type Props =
我想通过 gatsby 布局传递我所有的 props。例如: import React, { Component } from 'react'; export default class Exampl
如果我使用合成属性,那我为什么不直接说: self.property = nil; 这将释放引用计数,并确保我没有悬挂指针。 看起来很简单,但我看到的 99% 的代码似乎都是这样做的: [proper
Eslint 抛出 eslint(react/prop-types) 错误,尽管已经声明了 propTypes。我正在使用 eslint-plugin-react 我研究了其他几个类似的问题以及 li
我正在使用以下由 linter eslint-plugin-react 解析的代码。它返回警告: "product is missing in props validation" 当我在底部的 pro
我正在尝试在 React 应用程序中添加 TypeScript。 版本: "react": "16.9.0", "typescript": "3.5.3", 我有一个像这样的数组 import aLo
我有一个组件 . 如果组件没有 this.props.children , 我想设置 Prop ariaLabel作为isRequired ,否则 in 可以是可选的。我该怎么做? ariaLabe
我应该用一个代替另一个吗?一起使用它们更好吗?谢谢。 最佳答案 prop in obj 检查 obj 是否有名为 prop 的属性,即使它只是从原型(prototype)继承而来。 obj.hasOw
我的组件 Text有 2 个 Prop :isHideable: boolean和 hidden: boolean .我如何允许 Hidden仅在 isHideable 时作为 Prop 是true
我试图将带有一些 Prop 的功能组件发送到另一个组件,并在接收组件中尝试键入检查该组件的某些 Prop 。这是代码: // BaseButton.tsx export type ButtonProp
是否可以从也作为 prop 传递的未知组件推断出正确的 props 类型? 如果已知组件(存在于当前文件中),我可以获得 Prop : type ButtonProps = React.Compone
我对 react 还很陌生,这是我正在努力解决的问题。 有一个父组件 家长 它将 Prop 传递给 child 。 其中一个 Prop ,包括一个要渲染的元素,如下所示: 在子组件中,我想获取这个组
我想做一个 Tabs推断 active 的可能值的组件prop 基于它的 child 拥有的东西 name Prop 。这就是我尝试这样做的方式: import React from 'react'
我对 react 还很陌生,并且只有当用户开始向下滚动时,我才尝试将更多信息加载到记录数组中。问题是新信息出现在数组中,但如果您尝试调用它,它将返回undefined。我在这里不明白什么? 父组件:
因此,如果我对一个组件有很多不同的 Prop ,我希望我可以做类似的事情 const { ...props } = props; 而不是 const { prop1, prop2, prop3, ..
这是我寻求指导的问题类型,因为我不确定我正在寻找的内容是否存在...... 上下文:我正在使用 Firestore 来保存数据,并且正在构建一个可重用的自定义 Hook (React 16.8),以便
我有一个 React 组件,它获取一个配置对象作为 prop,它看起来像这样: { active: true, foo: { bar: 'baz' } } 在
如何附加另一个属性?我有一个 react 组件,其中有人传入 ...props,我想附加一个额外的 Prop 最佳答案 请记住,传递 Prop 的顺序将决定将哪个值传递给该组件。这适用于有两个同名
我是一名优秀的程序员,十分优秀!