Android 如何在React Native with TypeScript中编写适当的'Keyboard.addListener'?
我正在重写TypeScript的教程 它应该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
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;
...
注意,我添加了一个在属性之后,使用code>告诉TypeScript我确保在本例中,在componentDidMount()
希望这有帮助
...
export class Logo extends React.PureComponent {
constructor(props) {
super(props);
}
keyboardDidShowListener!: EmitterSubscription;
keyboardDidHideListener!: EmitterSubscription;
...