Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6213

React Native Typescript Formik typings of handleSubmit in onPress

$
0
0

I just started a project on React Native with Typescript and came along Formik in order to build form in my application.

I did the great formik tutorial guide and saw the use of formik with RN section in JS.

However, when I tried to translate this example into typescript, then I got the following typing error on the Button's onPress attribute:

No overload matches this call.  Overload 1 of 2, '(props: Readonly<ButtonProps>): Button', gave the following error.    Type '(e?: FormEvent<HTMLFormElement>) => 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>'.          Types of property 'nativeEvent' are incompatible.            Type 'NativeTouchEvent' is missing the following properties from type 'Event': bubbles, cancelBubble, cancelable, composed, and 17 more.  Overload 2 of 2, '(props: ButtonProps, context?: any): Button', gave the following error.    Type '(e?: FormEvent<HTMLFormElement>) => void' is not assignable to type '(ev: NativeSyntheticEvent<NativeTouchEvent>) => void'.ts(2769)index.d.ts(6926, 5): The expected type comes from property 'onPress' which is declared here on type 'IntrinsicAttributes & IntrinsicClassAttributes<Button> & Readonly<ButtonProps> & Readonly<{ children?: ReactNode; }>'index.d.ts(6926, 5): The expected type comes from property 'onPress' which is declared here on type 'IntrinsicAt...

From what I understand, the handleSubmit function (e?: FormEvent<HTMLFormElement>) => void, accepts an event from a form submission (onSubmit attribute of a form element). Since there is no Form equivalent in RN, it complains receiving the (ev: NativeSyntheticEvent<NativeTouchEvent>) => void from the RN Button's onPress attribute.

In order to get rid of the error and keep going, I used this following workaround which I'm not satisfied with :

<Button              title="Do it !"              color={colors.red}              onPress={                (handleSubmit as unknown) as (                  ev: NativeSyntheticEvent<NativeTouchEvent>                ) => void              }/>

I'd like to know how should I solve this typing error properly.

CodeSandbox: App.tsx:32

Thank you for your help.


Viewing all articles
Browse latest Browse all 6213

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>