英文:
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 = () => {
const handleKeyPress = (event) => {
const key = event.nativeEvent.key.toLowerCase();
console.log({key});
};
return (
<TextInput
onKeyPress={handleKeyPress}
...
/>
)
}
What is the type of event
parameter? In ReactJS I would use KeyboardEvent<HTMLInputElement>
what is the equivalent in React-Native?
答案1
得分: 1
尝试使用 NativeSyntheticEvent 和 TextInputKeyPressEventData
const handleKeyPress = (event: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
const key = event.nativeEvent.key.toLowerCase();
console.log({ key });
}
英文:
Try using NativeSyntheticEvent and TextInputKeyPressEventData
const handleKeyPress = (event: NativeSyntheticEvent<TextInputKeyPressEventData>) => {
const key = event.nativeEvent.key.toLowerCase();
console.log({ key });
答案2
得分: -1
const handleKeyPress = (event: KeyboardEvent
英文:
const handleKeyPress = (event: KeyboardEvent<TextInput>) => {
I hope this would help you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论