【发布时间】:2020-12-25 08:23:35
【问题描述】:
我有一个自定义 FieldInput 图标,我在其中使用 Native Base 的输入组件。我用它来验证 Formik。当我这样使用它时:
onChangeText={handleChange(fieldType)}
一切似乎都正常,但我在 onChangeText 上收到一个错误
Overload 1 of 2, '(props: Readonly<Input>): Input', gave the following error.
Type 'void' is not assignable to type '((text: string) => void) | undefined'.
当我把它改成
onChangeText={()=>handleChange(fieldType)}
错误消失了,但我的输入字段停止工作。我不能再输入它。我究竟做错了什么?我的组件如下所示:
type FieldInputProps = React.PropsWithRef<{
handleChange: (e: string) => void;
value: string;
fieldType: string;
placeholderText?: string;
style?: object;
rounded?: boolean;
}>;
export const FieldInput = React.forwardRef<Input, FieldInputProps>(({
handleChange,
fieldType,
placeholderText,
value,
style,
rounded,
}, ref) => {
return (
<Item rounded={rounded} style={[styles.inputItem, style]}>
<Input
ref={ref}
autoFocus={true}
autoCapitalize="none"
placeholder={placeholderText}
keyboardType="default"
onChangeText={handleChange(fieldType)}
value={value}
/>
</Item>
);
});
【问题讨论】:
-
您的 fieldType 未在箭头函数中定义。尝试发送事件。
-
你的意思是这样的?
(fieldType)=>handleChange(fieldType),我也试过了,但也没有用。你可以试试codesandbox吗? @kingneo
标签: javascript reactjs typescript react-native formik