react-native-navigation的迁移库

FirstTabScreen.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import React, {Component, PropTypes} from 'react';
  2. import {
  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 navigatorStyle = {
  15. statusBarColor: '#303F9F',
  16. toolBarColor: '#3F51B5',
  17. navigationBarColor: '#303F9F',
  18. buttonsTint: '#FFFFFF',
  19. titleColor: '#FFFFFF',
  20. tabSelectedTextColor: '#FFA000',
  21. tabNormalTextColor: '#FFC107',
  22. tabIndicatorColor: '#FFA000'
  23. };
  24. static navigatorButtons = {
  25. rightButtons: [
  26. {
  27. title: 'Edit',
  28. id: 'edit'
  29. },
  30. {
  31. icon: require('../../img/navicon_add.png'),
  32. title: 'Add',
  33. id: 'add'
  34. }
  35. ]
  36. };
  37. constructor(props) {
  38. super(props);
  39. // if you want to listen on navigator events, set this up
  40. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  41. }
  42. onNavigatorEvent(event) {
  43. switch (event.id) {
  44. case 'edit':
  45. Alert.alert('NavBar', 'Edit button pressed');
  46. break;
  47. case 'add':
  48. Alert.alert('NavBar', 'Add button pressed');
  49. break;
  50. default:
  51. console.log('Unhandled event ' + event.id);
  52. break;
  53. }
  54. }
  55. render() {
  56. return (
  57. <View style={{flex: 1, padding: 20}}>
  58. <Text style={styles.text}>
  59. <Text style={{fontWeight: '500'}}>Same Counter: </Text> {this.props.counter.count}
  60. </Text>
  61. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  62. <Text style={styles.button}>Increment Counter</Text>
  63. </TouchableOpacity>
  64. <TouchableOpacity onPress={ this.onPushPress.bind(this) }>
  65. <Text style={styles.button}>Push Screen</Text>
  66. </TouchableOpacity>
  67. <TouchableOpacity onPress={ this.onShowModalPress.bind(this) }>
  68. <Text style={styles.button}>Modal Screen</Text>
  69. </TouchableOpacity>
  70. <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
  71. <Text style={{fontWeight: '500'}}>Number prop: {this.props.num}</Text>
  72. <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text>
  73. <Text style={{fontWeight: '500'}}>Array prop: {this.props.obj.arr[0].str}</Text>
  74. </View>
  75. );
  76. }
  77. onIncrementPress() {
  78. this.props.dispatch(counterActions.increment());
  79. }
  80. onPushPress() {
  81. this.props.navigator.push({
  82. title: "More",
  83. screen: "example.PushedScreen",
  84. passProps: {
  85. passed: 'This is a prop passed in \'navigator.push()\'!',
  86. obj: {
  87. str: 'This is a prop passed in an object!',
  88. arr: [
  89. {
  90. str: 'This is a prop in an object in an array in an object!'
  91. }
  92. ]
  93. },
  94. num: 1234
  95. }
  96. });
  97. }
  98. onShowModalPress() {
  99. this.props.navigator.showModal({
  100. title: "Modal Screen",
  101. screen: "example.PushedScreen",
  102. passProps: {
  103. passed: 'This is a prop passed in \'navigator.showModal()\'!',
  104. obj: {
  105. str: 'This is a prop passed in an object!',
  106. arr: [
  107. {
  108. str: 'This is a prop in an object in an array in an object!'
  109. }
  110. ]
  111. },
  112. num: 1234
  113. }
  114. });
  115. }
  116. }
  117. const styles = StyleSheet.create({
  118. text: {
  119. textAlign: 'center',
  120. fontSize: 18,
  121. marginBottom: 10,
  122. marginTop:10
  123. },
  124. button: {
  125. textAlign: 'center',
  126. fontSize: 18,
  127. marginBottom: 10,
  128. marginTop:10,
  129. color: 'blue'
  130. }
  131. });
  132. // which props do we want to inject, given the global state?
  133. function mapStateToProps(state) {
  134. return {
  135. counter: state.counter
  136. };
  137. }
  138. export default connect(mapStateToProps)(FirstTabScreen);