react-native-navigation的迁移库

LifecycleScreen.js 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const React = require('react');
  2. const { Component } = require('react');
  3. const { View, Text, Button } = require('react-native');
  4. const { Navigation } = require('react-native-navigation');
  5. const testIDs = require('../testIDs');
  6. class LifecycleScreen extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.onClickPush = this.onClickPush.bind(this);
  10. this.state = {
  11. text: 'nothing yet'
  12. };
  13. this.subscription = Navigation.events().bindComponent(this);
  14. }
  15. componentDidAppear() {
  16. this.setState({ text: 'didAppear' });
  17. }
  18. componentDidDisappear() {
  19. alert('didDisappear'); // eslint-disable-line no-alert
  20. }
  21. componentWillUnmount() {
  22. this.subscription.remove();
  23. alert('componentWillUnmount'); // eslint-disable-line no-alert
  24. }
  25. navigationButtonPressed(id) {
  26. alert(`navigationButtonPressed: ${id}`); // eslint-disable-line no-alert
  27. }
  28. render() {
  29. return (
  30. <View style={styles.root}>
  31. <Text style={styles.h1}>{`Lifecycle Screen`}</Text>
  32. <Text style={styles.h1}>{this.state.text}</Text>
  33. <Button title='Push to test didDisappear' testID={testIDs.PUSH_TO_TEST_DID_DISAPPEAR_BUTTON} onPress={this.onClickPush} />
  34. {this.props.isModal ?
  35. (<Button title='Dismiss' testID={testIDs.DISMISS_MODAL_BUTTON} onPress={() => this.onClickDismiss()} />)
  36. : (<Button title='Pop' testID={testIDs.POP_BUTTON} onPress={() => this.onClickPop()} />)}
  37. <Text style={styles.footer}>{`this.props.componentId = ${this.props.componentId}`}</Text>
  38. </View>
  39. );
  40. }
  41. onClickPush() {
  42. Navigation.push(this.props.componentId, { component: { name: 'navigation.playground.TextScreen' } });
  43. }
  44. onClickPop() {
  45. Navigation.pop(this.props.componentId);
  46. }
  47. onClickDismiss() {
  48. Navigation.dismissModal(this.props.componentId);
  49. }
  50. }
  51. module.exports = LifecycleScreen;
  52. const styles = {
  53. root: {
  54. flexGrow: 1,
  55. justifyContent: 'center',
  56. alignItems: 'center',
  57. backgroundColor: '#f5fcff'
  58. },
  59. h1: {
  60. fontSize: 24,
  61. textAlign: 'center',
  62. margin: 10
  63. },
  64. footer: {
  65. fontSize: 10,
  66. color: '#888',
  67. marginTop: 10
  68. }
  69. };