在React-Native和TypeScript中,TextInput的键盘事件类型是什么?

huangapple go评论67阅读模式
英文:

What is the type of the keyboard event for TextInput in React-Native and TypeScript?

问题

event 参数的类型是什么?在 ReactJS 中,我会使用 KeyboardEvent<HTMLInputElement>,在 React-Native 中等价的是什么?

英文:

I'm not able to find this anywhere. I have code like this:

const Component = () =&gt; {

  const handleKeyPress = (event) =&gt; {
    const key = event.nativeEvent.key.toLowerCase();
    console.log({key});
  };

  return (
    &lt;TextInput
        onKeyPress={handleKeyPress}
        ...
    /&gt;
  )
}

What is the type of event parameter? In ReactJS I would use KeyboardEvent&lt;HTMLInputElement&gt; what is the equivalent in React-Native?

答案1

得分: 1

尝试使用 NativeSyntheticEventTextInputKeyPressEventData

const handleKeyPress = (event: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
    const key = event.nativeEvent.key.toLowerCase();
    console.log({ key });
}
英文:

Try using NativeSyntheticEvent and TextInputKeyPressEventData

const handleKeyPress = (event: NativeSyntheticEvent&lt;TextInputKeyPressEventData&gt;) =&gt; {
const key = event.nativeEvent.key.toLowerCase();
console.log({ key });

答案2

得分: -1

const handleKeyPress = (event: KeyboardEvent) => {

英文:
const handleKeyPress = (event: KeyboardEvent&lt;TextInput&gt;) =&gt; { 

I hope this would help you.

huangapple
  • 本文由 发表于 2023年7月3日 15:13:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定