如何使用 React 和 TypeScript 创建可重用的 Input 组件?

蒂姆

这是我第一次尝试 React TypeScript,并没有完全理解我在尝试构建可重用 Input 组件时遇到的错误。接近这种组件的 TypeScript 方式是什么?

请参阅下面的组件和错误:

import React, { FC, InputHTMLAttributes } from 'react';

interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
    name: string;
    label: string;
    ref: string;
}

const Input: FC<InputProps> = ({ name, label, ...otherProps }, ref) => {
    return (
        <label className={styles.formLabel}>
            {label}
            <input
                className={styles.formInput}
                {...otherProps}
                name={name}
                ref={ref}
            />
        </label>
    );
};

const FormInput = React.forwardRef(Input);

export default FormInput;

错误

TypeScript error in /Users/Timmchoover/Documents/Projects/evaly/evaly-app/src/before-login/components/forms/form-input/form-input.component.tsx(25,36):
Argument of type 'FC<InputProps>' is not assignable to parameter of type 'ForwardRefRenderFunction<unknown, InputProps>'.
  Types of property 'defaultProps' are incompatible.
    Type 'Partial<InputProps> | undefined' is not assignable to type 'undefined'.
      Type 'Partial<InputProps>' is not assignable to type 'undefined'.  TS2345

    23 | };
    24 | 
  > 25 | const FormInput = React.forwardRef(Input);
       |                                    ^
    26 | 
    27 | export default FormInput;
    28 |

史蒂文·坦

我认为FC这里不满足类型约束,请尝试ForwardRefRenderFunction

import React, { ForwardRefRenderFunction, InputHTMLAttributes } from 'react';

interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
    name: string;
    label: string;
    ref: string;
}

const Input: ForwardRefRenderFunction<HTMLInputElement, InputProps> = ({ name, label, ...otherProps }, ref) => {
    return (
        <label className={styles.formLabel}>
            {label}
            <input
                className={styles.formInput}
                {...otherProps}
                name={name}
                ref={ref}
            />
        </label>
    );
};

const FormInput = React.forwardRef(Input);

export default FormInput;

或者,您可以将Input合二为一FormInput让 TypeScript 为您推断:

const FormInput = React.forwardRef<HTMLInputElement, InputProps>(({ name, label, ...otherProps }, ref) => {
    ...
});


export default FormInput;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用React和Typescript创建具有不可分配类型错误的可重用按钮组件

在 React 中使用 Typescript 使组件可重用

如何在React中创建可重用的目录组件

可重用的模态组件 React Typescript

Typescript和React组件,均接受TextArea和Input的onChange

使用 Typescript 创建 React 组件

如何使用simpleSchema创建可重用组件

如何使用Ionic 3和Angular 4创建可重用组件

在React Native中创建可重用组件?

如何使用Redux,React和TypeScript在连接的组件中维护动作创建者类型?

如何制作可重用的React / MobX组件?

使用React Redux mapStateToProps的可重用组件

如何创建可重用的Form Vue组件

在使用 <slot/> 的组件内部使用 v-for 创建可重用和动态的 Tab 菜单

TypeScript&React:可重用的通用动作/组件

使用MVC创建可重用的UI组件

使用 scalatags 创建可重用的组件

React-Hooks:如何创建可重用的数据表组件?

React-Hooks:如何创建动态可重用数据表组件?

React-Hooks:如何创建具有动态内容的可重用数据表组件

如何在React中创建使用状态和道具的可重用函数

如何通过React和鼠标事件传播实现可重用组件?

如何从另一个可重用组件和容器组件访问可重用组件

如何使用Typescript和样式化组件创建引用

如何使用react和Typescript在父组件中呈现子组件?

React和Typescript:如何扩展通用组件道具?

使用 input[type=file] 动态加载和渲染 react 组件

如何使用react和typescript访问react功能组件中的props?

如何在React和Redux中重用组件?