react-native-navigation的迁移库

BackHandlerScreen.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const React = require('react');
  2. const { Component } = require('react');
  3. const { Navigation } = require('react-native-navigation');
  4. const { View, Text, Button, BackHandler } = require('react-native');
  5. class BackHandlerScreen extends Component {
  6. static get options() {
  7. return {
  8. topBar: {
  9. title: 'Back Handler',
  10. textColor: 'black',
  11. textFontSize: 16
  12. }
  13. };
  14. }
  15. constructor(props) {
  16. super(props);
  17. this.backHandler = () => {
  18. this.setState({
  19. backPress: 'Back button pressed!'
  20. });
  21. return true;
  22. };
  23. this.state = {
  24. backPress: ''
  25. };
  26. }
  27. render() {
  28. return (
  29. <View style={styles.root}>
  30. <Text style={styles.h1}>{`Back Handler Screen`}</Text>
  31. <Text style={styles.h2}>{this.state.backPress}</Text>
  32. <Button title='add back handler' onPress={() => this.addBackHandler()} />
  33. <Button title='remove back handler' onPress={() => this.removeBackHandler()} />
  34. <Button title='show single component modal' onPress={() => this.showModal()} />
  35. <Button title='show modal with stack' onPress={() => this.showModalWitchStack()} />
  36. </View>
  37. );
  38. }
  39. addBackHandler() {
  40. BackHandler.addEventListener('hardwareBackPress', this.backHandler);
  41. }
  42. removeBackHandler() {
  43. BackHandler.removeEventListener('hardwareBackPress', this.backHandler);
  44. }
  45. showModal() {
  46. Navigation.showModal({
  47. component: {
  48. name: 'navigation.playground.BackHandlerModalScreen'
  49. }
  50. });
  51. }
  52. showModalWitchStack() {
  53. Navigation.showModal({
  54. stack: {
  55. children: [
  56. {
  57. component: {
  58. name: 'navigation.playground.BackHandlerModalScreen'
  59. }
  60. },
  61. {
  62. component: {
  63. name: 'navigation.playground.BackHandlerModalScreen'
  64. }
  65. }
  66. ]
  67. }
  68. });
  69. }
  70. componentWillUnmount() {
  71. BackHandler.removeEventListener('hardwareBackPress', this.backHandler);
  72. }
  73. }
  74. const styles = {
  75. root: {
  76. flex: 1,
  77. backgroundColor: 'white',
  78. justifyContent: 'center',
  79. alignItems: 'center'
  80. },
  81. h2: {
  82. fontSize: 12,
  83. textAlign: 'center',
  84. margin: 10
  85. },
  86. footer: {
  87. fontSize: 10,
  88. color: '#888',
  89. marginTop: 10
  90. }
  91. };
  92. module.exports = BackHandlerScreen;