在React Native的Formik文档中,有一个示例表单:
然而,这会给我带来一个 TypeScript 错误:
<Formik initialValues={{ email: '' }} onSubmit={(values) => console.log(values)}>
{({ handleChange, handleBlur, handleSubmit, values }) => (
<View>
<TextInput onChangeText={handleChange('email')} onBlur={handleBlur('email')} value={values.email} />
<Button onPress={handleSubmit} title="Submit" />
</View>
)}
</Formik>
然而,这会给我带来一个 TypeScript 错误:
No overload matches this call.
Overload 1 of 2, '(props: Readonly<ButtonProps>): Button', gave the following error.
Type '(e?: FormEvent<HTMLFormElement> | undefined) => void' is not assignable to type '(ev: NativeSyntheticEvent<NativeTouchEvent>) => void'.
Types of parameters 'e' and 'ev' are incompatible.
Type 'NativeSyntheticEvent<NativeTouchEvent>' is not assignable to type 'FormEvent<HTMLFormElement>'.
鉴于我正在通过解构获取handleSubmit函数,如何正确地转换事件类型?
注意:我知道我可以这样做,但我已经读到这会导致React中的额外渲染:
<Button onPress={(e) => handleSubmit(e as any)} title="Submit" />
import { Button, TextInput } from 'react-native';
- mahi-man<Button onPress={(e) => handleSubmit(e as any)} title="提交" />
- mahi-man