Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在React Native with TypeScript中编写适当的'Keyboard.addListener'?_Android_Typescript_React Native_Expo - Fatal编程技术网

Android 如何在React Native with TypeScript中编写适当的'Keyboard.addListener'?

Android 如何在React Native with TypeScript中编写适当的'Keyboard.addListener'?,android,typescript,react-native,expo,Android,Typescript,React Native,Expo,我正在重写TypeScript的教程 它应该console.log在componentDidMount之后,但它没有。它也没有显示任何错误 我做错了什么 这是我的代码(为您最小化): 从“React”导入React; 从“react native”导入{视图、文本、动画、键盘}; 导出类徽标扩展React.PureComponent{ 建造师(道具){ 超级(道具); } componentDidMount(){ log(`嘿,我上马了!`); this.keyboardDidShowListe

我正在重写TypeScript的教程

它应该
console.log
componentDidMount
之后,但它没有。它也没有显示任何错误

我做错了什么

这是我的代码(为您最小化):

从“React”导入React;
从“react native”导入{视图、文本、动画、键盘};
导出类徽标扩展React.PureComponent{
建造师(道具){
超级(道具);
}
componentDidMount(){
log(`嘿,我上马了!`);
this.keyboardDidShowListener=Keyboard.addListener(
`键盘将显示`,
这个键盘会显示
);
this.keyboardDidHideListener=Keyboard.addListener(
`键盘将隐藏`,
这个键盘将隐藏
);
}
组件将卸载(){
this.keyboardDidShowListener.remove();
this.keyboardidHidelistener.remove();
}
键盘将显示=()=>{
log(`Keyboard shows`);
};
键盘将隐藏=()=>{
log(`Keyboard hides`);
};
render(){
返回(
请在此处键入金额
);
}
}
请帮忙


此处的教程代码:

您可以从react native导入
EmitterSubscription
接口,该接口是
Keyboard.addListener(…)
的返回类型

看起来像这样:
从'react native'导入{Keyboard,EmitterSubscription}

然后您可以将其添加到代码中:

...
export class Logo extends React.PureComponent {
  constructor(props) {
    super(props);
  }

  keyboardDidShowListener!: EmitterSubscription;
  keyboardDidHideListener!: EmitterSubscription;
...

注意,我添加了一个
告诉TypeScript我确保在本例中,在
componentDidMount()

希望这有帮助

...
export class Logo extends React.PureComponent {
  constructor(props) {
    super(props);
  }

  keyboardDidShowListener!: EmitterSubscription;
  keyboardDidHideListener!: EmitterSubscription;
...