react-native-navigation的迁移库

PushedScreen.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import React, {Component, PropTypes} from 'react';
  2. import {
  3. Text,
  4. View,
  5. ScrollView,
  6. TouchableOpacity,
  7. StyleSheet,
  8. TextInput
  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 PushedScreen extends Component {
  14. static navigatorStyle = {
  15. statusBarColor: '#303F9F',
  16. toolBarColor: '#3F51B5',
  17. navigationBarColor: '#303F9F',
  18. tabSelectedTextColor: '#FFA000',
  19. tabNormalTextColor: '#FFC107',
  20. tabIndicatorColor: '#FF4081'
  21. };
  22. static propTypes = {
  23. str: PropTypes.string.isRequired,
  24. obj: PropTypes.object.isRequired,
  25. num: PropTypes.number.isRequired
  26. };
  27. constructor(props) {
  28. super(props);
  29. this.bgColor = this.getRandomColor();
  30. console.log(`constructor ${this.bgColor}`);
  31. }
  32. componentWillUnmount() {
  33. console.log(`componentWillUnmount ${this.bgColor}`);
  34. }
  35. getRandomColor() {
  36. var letters = '0123456789ABCDEF'.split('');
  37. var color = '#';
  38. for (var i = 0; i < 6; i++) {
  39. color += letters[Math.floor(Math.random() * 16)];
  40. }
  41. return color;
  42. }
  43. render() {
  44. return (
  45. <View style={{flex: 1, padding: 20, backgroundColor: this.bgColor}}>
  46. <Text style={styles.text}>
  47. <Text style={{fontWeight: '500'}}>Counter: </Text> {this.props.counter.count}
  48. </Text>
  49. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  50. <Text style={styles.button}>Increment Counter</Text>
  51. </TouchableOpacity>
  52. <TouchableOpacity onPress={ this.onPushPress.bind(this) }>
  53. <Text style={styles.button}>Push Another</Text>
  54. </TouchableOpacity>
  55. <TouchableOpacity onPress={ this.onPopPress.bind(this) }>
  56. <Text style={styles.button}>Pop Screen</Text>
  57. </TouchableOpacity>
  58. <TouchableOpacity onPress={ this.onShowModalPress.bind(this) }>
  59. <Text style={styles.button}>Modal Screen</Text>
  60. </TouchableOpacity>
  61. <TouchableOpacity onPress={ this.onDismissModal.bind(this) }>
  62. <Text style={styles.button}>Dismiss modal</Text>
  63. </TouchableOpacity>
  64. <TouchableOpacity onPress={ this.onDismissAllModalsPress.bind(this) }>
  65. <Text style={styles.button}>Dismiss all modals</Text>
  66. </TouchableOpacity>
  67. <TouchableOpacity onPress={ this.onPopToRootPress.bind(this) }>
  68. <Text style={styles.button}>Pop to root</Text>
  69. </TouchableOpacity>
  70. <TouchableOpacity onPress={ this.onNewStackPress.bind(this) }>
  71. <Text style={styles.button}>New Stack</Text>
  72. </TouchableOpacity>
  73. <TextInput style={{height: 40, borderColor: 'gray', borderWidth: 1}}/>
  74. <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
  75. <Text style={{fontWeight: '500'}}>Number prop: {this.props.num}</Text>
  76. <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text>
  77. <Text style={{fontWeight: '500'}}>Array prop: {this.props.obj.arr[0].str}</Text>
  78. </View>
  79. );
  80. }
  81. onIncrementPress() {
  82. this.props.dispatch(counterActions.increment());
  83. }
  84. onPushPress() {
  85. this.props.navigator.push({
  86. title: "More",
  87. screen: "example.PushedScreen",
  88. passProps: {
  89. passed: 'This is a prop passed in \'navigator.push()\'!',
  90. obj: {
  91. str: 'This is a prop passed in an object!',
  92. arr: [
  93. {
  94. str: 'This is a prop in an object in an array in an object!'
  95. }
  96. ]
  97. },
  98. num: 1234
  99. }
  100. });
  101. }
  102. onPopPress() {
  103. this.props.navigator.pop();
  104. }
  105. onShowModalPress() {
  106. this.props.navigator.showModal({
  107. title: "Modal Screen",
  108. screen: "example.PushedScreen",
  109. passProps: {
  110. passed: 'This is a prop passed in \'navigator.showModal()\'!',
  111. obj: {
  112. str: 'This is a prop passed in an object!',
  113. arr: [
  114. {
  115. str: 'This is a prop in an object in an array in an object!'
  116. }
  117. ]
  118. },
  119. num: 1234
  120. }
  121. });
  122. }
  123. onDismissAllModalsPress() {
  124. this.props.navigator.dismissAllModals();
  125. }
  126. onDismissModal() {
  127. this.props.navigator.dismissModal();
  128. }
  129. onPopToRootPress() {
  130. this.props.navigator.popToRoot();
  131. }
  132. onNewStackPress() {
  133. this.props.navigator.resetTo({
  134. title: "NEW STACK",
  135. screen: "example.PushedScreen",
  136. passProps: {
  137. passed: 'This is a prop passed in \'navigator.push()\'!',
  138. obj: {
  139. str: 'This is a prop passed in an object!',
  140. arr: [
  141. {
  142. str: 'This is a prop in an object in an array in an object!'
  143. }
  144. ]
  145. },
  146. num: 1234
  147. }
  148. });
  149. }
  150. }
  151. const styles = StyleSheet.create({
  152. text: {
  153. textAlign: 'center',
  154. fontSize: 18,
  155. marginBottom: 10,
  156. marginTop: 10
  157. },
  158. button: {
  159. textAlign: 'center',
  160. fontSize: 18,
  161. marginBottom: 10,
  162. marginTop: 10,
  163. color: 'blue'
  164. }
  165. });
  166. // which props do we want to inject, given the global state?
  167. function mapStateToProps(state) {
  168. return {
  169. counter: state.counter
  170. };
  171. }
  172. export default connect(mapStateToProps)(PushedScreen);