反应本机|如何使用Typescript访问键盘的endCoordinates并进行重组

时间:2019-02-07 15:31:51

标签: javascript typescript react-native higher-order-components recompose

因此,我在我的react本机应用程序中使用recompoose和typescript,并且尝试访问Keyboard的endCoordinates以获取Keyboard高度。我关注了this和这篇post的文章,但是我无法访问endCoordinates,它始终是未定义的。

这是我尝试过的:

const withLifeCycle = lifecycle<Handlers, {}> ({
     componentDidMount() {
        // @ts-ignore
        this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this.props._keyboardDidShow)
},
 componentWillUnmount() {
   // @ts-ignore
   this.keyboardDidShowListener.remove();
 }
})    


interface Handlers {
  _keyboardDidShow: (e:any) => void;
}
// WrappedProps have some other props for some other part of codes

export const enhance = compose<WrappedProps, Props>(
withHandlers<
Props,
{}
>({
  _keyboardDidShow: (e) => () =>{
     console.log(e.endCoordinates) // This is Undefined
   }
 }),
 withLifeCycle
);

我认为问题是我需要将keyboardDidShow事件类型传递给方法的方式,因为e对象没有任何endCoordinates

1 个答案:

答案 0 :(得分:0)

KeyboardEvent导入react-native

const _keyboardDidShow = (props: KeyboardEvent)

const { endCoordinates } = props;