react-native-navigation的迁移库

FirstTabScreen.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import React, {
  2. Component,
  3. Text,
  4. View,
  5. ScrollView,
  6. TouchableOpacity,
  7. StyleSheet,
  8. Alert
  9. } from 'react-native';
  10. import { connect } from 'react-redux';
  11. import * as counterActions from '../reducers/counter/actions';
  12. // this is a traditional React component connected to the redux store
  13. class FirstTabScreen extends Component {
  14. static navigatorButtons = {
  15. rightButtons: [
  16. {
  17. title: 'Edit',
  18. id: 'edit'
  19. },
  20. {
  21. icon: require('../../img/navicon_add.png'),
  22. title: 'Add',
  23. id: 'add'
  24. }
  25. ]
  26. };
  27. constructor(props) {
  28. super(props);
  29. // if you want to listen on navigator events, set this up
  30. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  31. }
  32. onNavigatorEvent(event) {
  33. switch (event.id) {
  34. case 'edit':
  35. Alert.alert('NavBar', 'Edit button pressed');
  36. break;
  37. case 'add':
  38. Alert.alert('NavBar', 'Add button pressed');
  39. break;
  40. default:
  41. console.log('Unhandled event ' + event.id);
  42. break;
  43. }
  44. }
  45. render() {
  46. return (
  47. <View style={{flex: 1, padding: 20}}>
  48. <Text style={styles.text}>
  49. <Text style={{fontWeight: '500'}}>Same Counter: </Text> {this.props.counter.count}
  50. </Text>
  51. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  52. <Text style={styles.button}>Increment Counter</Text>
  53. </TouchableOpacity>
  54. <TouchableOpacity onPress={ this.onPushPress.bind(this) }>
  55. <Text style={styles.button}>Push Screen</Text>
  56. </TouchableOpacity>
  57. </View>
  58. );
  59. }
  60. onIncrementPress() {
  61. this.props.dispatch(counterActions.increment());
  62. }
  63. onPushPress() {
  64. this.props.navigator.push({
  65. title: "More",
  66. screen: "example.PushedScreen"
  67. });
  68. }
  69. }
  70. const styles = StyleSheet.create({
  71. text: {
  72. textAlign: 'center',
  73. fontSize: 18,
  74. marginBottom: 10,
  75. marginTop:10
  76. },
  77. button: {
  78. textAlign: 'center',
  79. fontSize: 18,
  80. marginBottom: 10,
  81. marginTop:10,
  82. color: 'blue'
  83. }
  84. });
  85. // which props do we want to inject, given the global state?
  86. function mapStateToProps(state) {
  87. return {
  88. counter: state.counter
  89. };
  90. }
  91. export default connect(mapStateToProps)(FirstTabScreen);