react-native-navigation的迁移库

ContainerWrapper.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import _ from 'lodash';
  2. import React, { Component } from 'react';
  3. export default class ContainerWrapper {
  4. static wrap(containerName, OriginalContainer, store) {
  5. return class extends Component {
  6. constructor(props) {
  7. super(props);
  8. this._assertId(props);
  9. this._createState(props);
  10. }
  11. _assertId(props) {
  12. if (!props.id) {
  13. throw new Error(`Container ${containerName} does not have an id!`);
  14. }
  15. }
  16. _createState(props) {
  17. this.state = {
  18. id: props.id,
  19. allProps: _.merge({}, props, store.getPropsForContainerId(props.id))
  20. };
  21. }
  22. componentWillMount() {
  23. store.setRefForId(this.state.id, this);
  24. }
  25. componentWillUnmount() {
  26. store.cleanId(this.state.id);
  27. }
  28. onStart() {
  29. if (this.originalContainerRef.onStart) {
  30. this.originalContainerRef.onStart();
  31. }
  32. }
  33. onStop() {
  34. if (this.originalContainerRef.onStop) {
  35. this.originalContainerRef.onStop();
  36. }
  37. }
  38. componentWillReceiveProps(nextProps) {
  39. this.setState({
  40. allProps: _.merge({}, nextProps, store.getPropsForContainerId(this.state.id))
  41. });
  42. }
  43. render() {
  44. return (
  45. <OriginalContainer
  46. ref={(r) => this.originalContainerRef = r}
  47. {...this.state.allProps}
  48. id={this.state.id}
  49. key={this.state.id}
  50. />
  51. );
  52. }
  53. };
  54. }
  55. }