react-native-navigation的迁移库

FirstTabScreen.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. static propTypes = {
  38. str: PropTypes.string.isRequired,
  39. obj: PropTypes.object.isRequired,
  40. num: PropTypes.number.isRequired
  41. };
  42. constructor(props) {
  43. super(props);
  44. // if you want to listen on navigator events, set this up
  45. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  46. }
  47. onNavigatorEvent(event) {
  48. switch (event.id) {
  49. case 'edit':
  50. Alert.alert('NavBar', 'Edit button pressed');
  51. break;
  52. case 'add':
  53. Alert.alert('NavBar', 'Add button pressed');
  54. break;
  55. default:
  56. console.log('Unhandled event ' + event.id);
  57. break;
  58. }
  59. }
  60. render() {
  61. return (
  62. <View style={{flex: 1, padding: 20}}>
  63. <Text style={styles.text}>
  64. <Text style={{fontWeight: '500'}}>Same Counter: </Text> {this.props.counter.count}
  65. </Text>
  66. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  67. <Text style={styles.button}>Increment Counter</Text>
  68. </TouchableOpacity>
  69. <TouchableOpacity onPress={ this.onPushPress.bind(this) }>
  70. <Text style={styles.button}>Push Screen</Text>
  71. </TouchableOpacity>
  72. <TouchableOpacity onPress={ this.onShowModalPress.bind(this) }>
  73. <Text style={styles.button}>Modal Screen</Text>
  74. </TouchableOpacity>
  75. <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
  76. <Text style={{fontWeight: '500'}}>Number prop: {this.props.num}</Text>
  77. <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text>
  78. <Text style={{fontWeight: '500'}}>Array prop: {this.props.obj.arr[0].str}</Text>
  79. </View>
  80. );
  81. }
  82. onIncrementPress() {
  83. this.props.dispatch(counterActions.increment());
  84. }
  85. onPushPress() {
  86. this.props.navigator.push({
  87. title: "More",
  88. screen: "example.PushedScreen",
  89. passProps: {
  90. passed: 'This is a prop passed in \'navigator.push()\'!',
  91. obj: {
  92. str: 'This is a prop passed in an object!',
  93. arr: [
  94. {
  95. str: 'This is a prop in an object in an array in an object!'
  96. }
  97. ]
  98. },
  99. num: 1234
  100. }
  101. });
  102. }
  103. onShowModalPress() {
  104. this.props.navigator.showModal({
  105. title: "Modal Screen",
  106. screen: "example.PushedScreen",
  107. passProps: {
  108. passed: 'This is a prop passed in \'navigator.showModal()\'!',
  109. obj: {
  110. str: 'This is a prop passed in an object!',
  111. arr: [
  112. {
  113. str: 'This is a prop in an object in an array in an object!'
  114. }
  115. ]
  116. },
  117. num: 1234
  118. }
  119. });
  120. }
  121. }
  122. const styles = StyleSheet.create({
  123. text: {
  124. textAlign: 'center',
  125. fontSize: 18,
  126. marginBottom: 10,
  127. marginTop:10
  128. },
  129. button: {
  130. textAlign: 'center',
  131. fontSize: 18,
  132. marginBottom: 10,
  133. marginTop:10,
  134. color: 'blue'
  135. }
  136. });
  137. // which props do we want to inject, given the global state?
  138. function mapStateToProps(state) {
  139. return {
  140. counter: state.counter
  141. };
  142. }
  143. export default connect(mapStateToProps)(FirstTabScreen);