I am trying to create a custom component with react native TextInput component, and passing onChange props down to TextInput alongside some other code, but the event
type is wrong according to typescript.
CustomComponent:
import type { TextInputProps } from "react-native";
interface InputProps extends InputHTMLAttributes<TextInputProps> {
errorMsg?: string;
labelText?: string;
appearance?: "primary" | "rounded";
}
export default function Input(props: HTMLInputElement, InputProps) {
return
<TextInput
placeholder=(props.placeholder)
onChange={(e) => {
setText(e.nativeEvent.text)
if(props.onChange) props.onChange(e)
}}
/>
}
But typescript says:
Argument of type 'NativeSyntheticEvent<TextInputChangeEventData>' is not assignable to parameter of type 'ChangeEvent<TextInputProps>'.
Types of property 'target' are incompatible.
Type 'Component<unknown, {}, any> & Readonly<NativeMethods>' is not assignable to type 'EventTarget & TextInputProps'.
Type 'Component<unknown, {}, any> & Readonly<NativeMethods>' is missing the following properties from type 'EventTarget': addEventListener, dispatchEvent, removeEventListenerts(2345)
(parameter) e: NativeSyntheticEvent<TextInputChangeEventData>
Tried changing types and looking online for similar problems but no success
2
Answers
I feel like this is what you’re looking for. Since TextInput is already a component, you just want to extend it’s props.
Try with
onChangeText={newText => setText(newText)}
instead ofonChange
.