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

TS: custom onSubmit do not match React Hook Form types

$
0
0

I'm geting type's problems when I try to use SubmitHandler type to typping onSubmit prop, I'm getting:

TS2345: Argument of type 'SubmitHandler<T>' is not assignable to parameter of type 'SubmitHandler<FieldValues>'. Type 'FieldValues' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'FieldValues'.

Implementation

import {  FieldValues, useFormContext,  SubmitHandler } from 'react-hook-form';import { Pressable, Text } from 'react-native';type ButtonProps<T extends FieldValues> = PressableProps & {  onSubmit: SubmitHandler<T>;};function SubmitButton<T = FieldValues>({ onSubmit, ...RestButtonProps }: ButtonProps<T>) {  const { handleSubmit } = useFormContext();  return (<Pressable {...RestButtonProps} onPress={() => handleSubmit(onSubmit)}><Text>Submit</Text></Pressable>  );}type LoginFormValues = {  email: string;  password: string;};function LoginForm() {  const handleSubmit: SubmitHandler<LoginFormValues> = ({ email, password }) =>    console.warn(email, password);  return (<SubmitButton<LoginFormValues> title="Sign In" onSubmit={handleSubmit} />  );}

Viewing all articles
Browse latest Browse all 6287

Trending Articles



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