react-native-navigation的迁移库

PushedScreen.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import _ from 'lodash';
  2. import React, { Component } from 'react';
  3. import {
  4. StyleSheet,
  5. View,
  6. Text,
  7. Button
  8. } from 'react-native';
  9. import Navigation from 'react-native-navigation';
  10. class PushedScreen extends Component {
  11. static navigationStyle = {
  12. title: 'Static Title'
  13. }
  14. constructor(props) {
  15. super(props);
  16. this.onClickPush = this.onClickPush.bind(this);
  17. this.onClickPop = this.onClickPop.bind(this);
  18. this.onClickPopPrevious = this.onClickPopPrevious.bind(this);
  19. this.onClickPopToFirstPosition = this.onClickPopToFirstPosition.bind(this);
  20. this.onClickPopToRoot = this.onClickPopToRoot.bind(this);
  21. }
  22. render() {
  23. const stackPosition = this.getStackPosition();
  24. return (
  25. <View style={styles.root}>
  26. <Text style={styles.h1}>{`Pushed Screen`}</Text>
  27. <Text style={styles.h2}>{`Stack Position: ${stackPosition}`}</Text>
  28. <Button title="Push" onPress={this.onClickPush} />
  29. <Button title="Pop" onPress={this.onClickPop} />
  30. <Button title="Pop Previous" onPress={this.onClickPopPrevious} />
  31. <Button title="Pop To Root" onPress={this.onClickPopToRoot} />
  32. {stackPosition > 2 && <Button title="Pop To Stack Position 1" onPress={this.onClickPopToFirstPosition} />}
  33. <Text style={styles.footer}>{`this.props.id = ${this.props.id}`}</Text>
  34. </View>
  35. );
  36. }
  37. onClickPush() {
  38. Navigation.on(this.props.id).push({
  39. name: 'navigation.playground.PushedScreen',
  40. passProps: {
  41. stackPosition: this.getStackPosition() + 1,
  42. previousScreenIds: _.concat([], this.props.previousScreenIds || [], this.props.id)
  43. }
  44. });
  45. }
  46. onClickPop() {
  47. Navigation.on(this.props.id).pop();
  48. }
  49. onClickPopPrevious() {
  50. Navigation.on(_.last(this.props.previousScreenIds)).pop();
  51. }
  52. onClickPopToFirstPosition() {
  53. Navigation.on(this.props.id).popTo(this.props.previousScreenIds[0]);
  54. }
  55. onClickPopToRoot() {
  56. Navigation.on(this.props.id).popToRoot();
  57. }
  58. getStackPosition() {
  59. return this.props.stackPosition || 1;
  60. }
  61. }
  62. const styles = {
  63. root: {
  64. flexGrow: 1,
  65. justifyContent: 'center',
  66. alignItems: 'center',
  67. backgroundColor: '#f5fcff'
  68. },
  69. h1: {
  70. fontSize: 24,
  71. textAlign: 'center',
  72. margin: 10
  73. },
  74. h2: {
  75. fontSize: 12,
  76. textAlign: 'center',
  77. margin: 10
  78. },
  79. footer: {
  80. fontSize: 10,
  81. color: '#888',
  82. marginTop: 10
  83. }
  84. };
  85. export default PushedScreen;