react-native-navigation的迁移库

BackHandlerScreen.js 2.3KB

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