Handling Back Event2019-01-02 23:32Dev, JavaScript, React Native

React Native: Handling Back Event

import { View, Text, FlatList, BackHandler } from 'react-native';
 
componentDidMount() {
  BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}
 
componentWillUnmount() {
  BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}
 
handleBackPress = () => {
  console.log(123);
  BackHandler.exitApp();
  return true;
}
 
render(){
    ...
    <Toolbar
      leftElement="arrow-back"
      onLeftElementPress={() => this.handleBackPress()}
      centerElement="Donner Helper"
    />
    ...
}

上方的代码将这个方法绑定到了 Toolbar 的点击事件中

之后点击虚拟按键或者实体按键都会调用 handleBackPress() 方法

Powered by Remix
|
Designed by szhshp
|
Copyright © szhshp 2022