react-native-navigation的迁移库

SecondTabScreen.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import React, {Component, PropTypes} from 'react';
  2. import {
  3. Text,
  4. Image,
  5. View,
  6. ScrollView,
  7. TouchableOpacity,
  8. StyleSheet,
  9. Alert
  10. } from 'react-native';
  11. import { connect } from 'react-redux';
  12. import * as counterActions from '../reducers/counter/actions';
  13. // this is a traditional React component connected to the redux store
  14. class SecondTabScreen extends Component {
  15. static navigatorStyle = {
  16. drawUnderNavBar: true,
  17. drawUnderTabBar: true,
  18. navBarTranslucent: true
  19. };
  20. constructor(props) {
  21. super(props);
  22. this.buttonsCounter = 0;
  23. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  24. }
  25. render() {
  26. return (
  27. <ScrollView style={{flex: 1}}>
  28. <Image style={{width: undefined, height: 100}} source={require('../../img/colors.png')} />
  29. <View style={{padding: 20}}>
  30. <Text style={styles.text}>
  31. <Text style={{fontWeight: '500'}}>Here Too: </Text> {this.props.counter.count}
  32. </Text>
  33. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  34. <Text style={styles.button}>Increment Counter</Text>
  35. </TouchableOpacity>
  36. <TouchableOpacity onPress={ this.onSelectFirstTabPress.bind(this) }>
  37. <Text style={styles.button}>Select First Tab</Text>
  38. </TouchableOpacity>
  39. <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
  40. <Text style={{fontWeight: '500'}}>Number prop: {this.props.num}</Text>
  41. <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text>
  42. <Text style={{fontWeight: '500'}}>Array prop: {this.props.obj.arr[0].str}</Text>
  43. <TouchableOpacity onPress={ this.onSetButton.bind(this) }>
  44. <Text style={styles.button}>Set a button</Text>
  45. </TouchableOpacity>
  46. </View>
  47. </ScrollView>
  48. );
  49. }
  50. onIncrementPress() {
  51. this.props.dispatch(counterActions.increment());
  52. }
  53. onSelectFirstTabPress() {
  54. this.props.navigator.switchToTab({
  55. tabIndex: 0
  56. })
  57. }
  58. onSetButton() {
  59. this.props.navigator.setButtons({
  60. rightButtons: [
  61. {
  62. title: 'Right',
  63. icon: require('../../img/navicon_add.png'),
  64. id: 'right'
  65. }
  66. ],
  67. leftButtons: [
  68. {
  69. title: 'Left',
  70. icon: require('../../img/navicon_add.png'),
  71. id: 'left'
  72. }
  73. ]
  74. });
  75. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  76. }
  77. onNavigatorEvent(event) {
  78. if (event.type == 'DeepLink') {
  79. this.handleDeepLink(event);
  80. } else {
  81. switch (event.id) {
  82. case 'left':
  83. Alert.alert('NavBar', 'Left button pressed');
  84. break;
  85. case 'right':
  86. Alert.alert('NavBar', 'Right button pressed');
  87. break;
  88. }
  89. }
  90. console.log('ListScreen', 'Unhandled event ' + event.id);
  91. }
  92. handleDeepLink(event) {
  93. const parts = event.link.split('/');
  94. if (parts[0] == 'tab2') {
  95. if (parts[1] == 'select') {
  96. this.props.navigator.switchToTab({});
  97. }
  98. if (parts[1] == 'pushScreen') {
  99. this.pushScreenFromSideMenu();
  100. }
  101. }
  102. }
  103. pushScreenFromSideMenu() {
  104. this.props.navigator.toggleDrawer({
  105. side: 'left',
  106. animated: true,
  107. to: 'closed'
  108. });
  109. this.props.navigator.push({
  110. title: "Pushed from SideMenu",
  111. screen: parts[2],
  112. passProps: {
  113. str: 'This is a prop passed in \'navigator.push()\'!',
  114. obj: {
  115. str: 'This is a prop passed in an object!',
  116. arr: [
  117. {
  118. str: 'This is a prop in an object in an array in an object!'
  119. }
  120. ]
  121. },
  122. num: 1234
  123. }
  124. });
  125. }
  126. }
  127. const styles = StyleSheet.create({
  128. text: {
  129. textAlign: 'center',
  130. fontSize: 18,
  131. marginBottom: 10,
  132. marginTop:10,
  133. },
  134. button: {
  135. textAlign: 'center',
  136. fontSize: 18,
  137. marginBottom: 10,
  138. marginTop:10,
  139. color: 'blue'
  140. }
  141. });
  142. // which props do we want to inject, given the global state?
  143. function mapStateToProps(state) {
  144. return {
  145. counter: state.counter
  146. };
  147. }
  148. export default connect(mapStateToProps)(SecondTabScreen);