reactjs - React-Form(v7) Reactstrap 与 Typescript
问题描述
我的打字稿表现得很奇怪,需要帮助来解决它。
我正在尝试将redux-form(v7)与reactstrap一起使用
表格.tsx
<Field
type="text"
component={InputField}
/>
<Field
type="password"
component={InputField}
/>
输入字段
import {Input} from 'reactstrap';
import {WrappedFieldProps} from 'redux-form/lib/Field';
type Props = {
type?: string;
}
const InputField = (props: WrappedFieldProps & Props) => {
const { type, input } = props;
return(
<Input
type={type}
{...input}
/>
);
};
在那种情况下,我得到了 Typescript 错误:TS2322
Types of property 'type' are incompatible.
Type 'string | undefined' is not assignable to type '"number" | "select" | "textarea" | "text" | "hidden" | "color" | "email" | "file" | "radio" | "ch...'.
Type 'string' is not assignable to type '"number" | "select" | "textarea" | "text" | "hidden" | "color" | "email" | "file" | "radio" | "ch...'.
但是,如果我更改type?: string
为type?: InputType;
( type from reactstrap import {InputType} from 'reactstrap/lib/Input';
),那么它会解决InputField中的问题,但随后我会在Form.tsx中遇到相同的错误
解决方案
一个老问题,但我也在寻找这个问题的答案。我发现的一些解决方案:
<Input type={"text" as any} />
或者
import { Input } from 'reactstrap';
import { InputType } from 'reactstrap/lib/Input'
let type:InputType = "text" // You can use Ctrl+Space on VSC for "enum"'d values
<Input type={type} />
或者
import { Input, InputProps, ButtonProps } from 'reactstrap';
type InputElement = {
[name:string]:InputProps
}
type BtnElement = {
[name:string]:InputProps
}
let inputElement:InputElement = {
login: {
onChange: this.handleChange,
onKeyDown: this.handleKeydown,
value: this.state.username,
type: "text",
placeholder: "Username",
name: "username"
}
}
let btnElement:BtnElement = {
submitBtn:{
onClick:() => this.submit(),
}
}
<Input {...inputElement.login} />
<Button {...btnElement.submitBtn}>Login</Button>
推荐阅读
- jenkins - Jenkins 不调用 psexec 命令,因为“访问被拒绝”
- boost - Boost进程连续读取输出
- corda - 如何在 Corda 中添加自定义类型的货币及其价值?
- angular - 拒绝设置不安全的标头“Access-Control-Request-Headers”Angular 4
- php - zend - 参数太少而无法运行
- e-commerce - BigCommerce Customize checkout page
- javascript - 使用 text 方法获取元素的文本,然后检查条件但没有得到适当的输出
- c# - 为什么 RU 的数量会改变我的 CosmosDB 结果集大小?
- r - 每个 ID 多次观察,为最后一次观察创建列
- ruby-on-rails - Rails db 迁移错误