Jam*_*ber 4 javascript typescript reactjs
我试图将我的 keyup 事件与 TS 一起使用,但使用any|KeyboardEvent不起作用。
如何在 React 中输入事件?
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
Run Code Online (Sandbox Code Playgroud)
<input
type="text"
placeholder="Search for claim ID"
onKeyUp={(e: KeyboardEvent) => {
console.info();
}}
/>
Run Code Online (Sandbox Code Playgroud)
Apo*_*los 14
首先,正如 Bao Huynh Lam 在评论中所说,你需要使用这个
onKeyUp={(e: React.KeyboardEvent<HTMLInputElement>) => {
Run Code Online (Sandbox Code Playgroud)
作为事件类型。然后,为了获取输入的值,您可以强制转换e.target为HTMLInputElement,然后获取其值。
检查这个小沙箱
<input
type="text"
placeholder="Search for claim ID"
onKeyUp={(e: React.KeyboardEvent<HTMLInputElement>) => {
console.info((e.target as HTMLInputElement).value);
}}
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12631 次 |
| 最近记录: |