Loadding..

Tạo action BackAndroid trong React native

Tạo action BackAndroid trong React native

Trên các smartphone android thường sẽ có 1 nút back vật lý ngay trên màn hình. Khi làm việc trong react-native chúng ta có thể tạo action cho nó hoạt động được với app. Dưới đây là đoạn code giúp bạn thực hiện điều đó trong react-native

 

import React, { Component } from 'react';  
import {  
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigator,
  BackAndroid
} from 'react-native';

import Home from './components/Home';  
import Settings from './components/Settings';

var _navigator;

/* 
  Android hardware back button. Without this, android device back button prese
  will close the app directly if you are on settings screeb without going back to
  home.
 */
BackAndroid.addEventListener('hardwareBackPress', () => {  
  if (_navigator && _navigator.getCurrentRoutes().length > 1) {
    _navigator.pop();
    return true;
  }
  return false;
});

class ToolbarNav extends Component {

  _renderScene (route, navigator) {
    _navigator = navigator;
    switch (route.id) {
      case 'home':
        return (
          
        )
      case 'settings':
        return (
          
        )
    }
  }

  render() {
    return (
       this._renderScene(route, navigator)}
      />
    );
  }
}

AppRegistry.registerComponent('ToolbarNav', () => ToolbarNav);  

Chúc bạn thành công !

Print
Image

enqtran

I'm enqtran - A coder and blogger :) [email protected]