javascript - Escribiendo un keyup de un input en React TypeScript
Estoy tratando de poner mi evento keyup con TS, pero usar any|KeyboardEvent no funciona.
¿Cómo escribir eventos en React?
Error TS
Type '(e: KeyboardEvent) => void' is not assignable to type 'KeyboardEventHandler<HTMLInputElement>'.
Types of parameters 'e' and 'event' are incompatible.
Type 'KeyboardEvent<HTMLInputElement>' is missing the following properties from type 'KeyboardEvent': char, isComposing, DOM_KEY_LOCATION_LEFT, DOM_KEY_LOCATION_NUMPAD, and 15 more. TS2322
Componente
<input
type="text"
placeholder="Search for claim ID"
onKeyUp={(e: KeyboardEvent) => {
console.info();
}}
/>
Respuesta #1
En primer lugar, como Bao Huynh Lam afirmó en los comentarios, necesitas usar esto
onKeyUp={(e: React.KeyboardEvent<HTMLInputElement>) => {
como tipo de evento. Luego, para obtener el valor de la entrada, puedes convertir e.target en HTMLInputElement y luego obtener su valor.
Revisa este pequeño arenero
<input
type="text"
placeholder="Search for claim ID"
onKeyUp={(e: React.KeyboardEvent<HTMLInputElement>) => {
console.info((e.target as HTMLInputElement).value);
}}
/>
