react-native-navigation的迁移库

SecondTabScreen.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }
  24. render() {
  25. return (
  26. <ScrollView style={{flex: 1}}>
  27. <Image style={{width: undefined, height: 100}} source={require('../../img/colors.png')} />
  28. <View style={{padding: 20}}>
  29. <Text style={styles.text}>
  30. <Text style={{fontWeight: '500'}}>Here Too: </Text> {this.props.counter.count}
  31. </Text>
  32. <TouchableOpacity onPress={ this.onIncrementPress.bind(this) }>
  33. <Text style={styles.button}>Increment Counter</Text>
  34. </TouchableOpacity>
  35. <Text style={{fontWeight: '500'}}>String prop: {this.props.str}</Text>
  36. <Text style={{fontWeight: '500'}}>Number prop: {this.props.num}</Text>
  37. <Text style={{fontWeight: '500'}}>Object prop: {this.props.obj.str}</Text>
  38. <Text style={{fontWeight: '500'}}>Array prop: {this.props.obj.arr[0].str}</Text>
  39. <TouchableOpacity onPress={ this.onSetButton.bind(this) }>
  40. <Text style={styles.button}>Set a button</Text>
  41. </TouchableOpacity>
  42. </View>
  43. </ScrollView>
  44. );
  45. }
  46. onIncrementPress() {
  47. this.props.dispatch(counterActions.increment());
  48. }
  49. onSetButton() {
  50. this.props.navigator.setButtons({
  51. rightButtons: [
  52. {
  53. title: 'Right',
  54. icon: require('../../img/navicon_add.png'),
  55. id: 'right'
  56. }
  57. ],
  58. leftButtons: [
  59. {
  60. title: 'Left',
  61. icon: require('../../img/navicon_add.png'),
  62. id: 'left'
  63. }
  64. ]
  65. });
  66. this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
  67. }
  68. onNavigatorEvent(event) {
  69. switch (event.id) {
  70. case 'left':
  71. Alert.alert('NavBar', 'Left button pressed');
  72. break;
  73. case 'right':
  74. Alert.alert('NavBar', 'Right button pressed');
  75. break;
  76. }
  77. }
  78. }
  79. const styles = StyleSheet.create({
  80. text: {
  81. textAlign: 'center',
  82. fontSize: 18,
  83. marginBottom: 10,
  84. marginTop:10,
  85. },
  86. button: {
  87. textAlign: 'center',
  88. fontSize: 18,
  89. marginBottom: 10,
  90. marginTop:10,
  91. color: 'blue'
  92. }
  93. });
  94. // which props do we want to inject, given the global state?
  95. function mapStateToProps(state) {
  96. return {
  97. counter: state.counter
  98. };
  99. }
  100. export default connect(mapStateToProps)(SecondTabScreen);