react-native-navigation的迁移库

ContainerWrapper.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. componentWillReceiveProps(nextProps) {
  29. this.setState({
  30. allProps: _.merge({}, nextProps, store.getPropsForContainerId(this.state.id))
  31. });
  32. }
  33. render() {
  34. return (
  35. <OriginalContainer
  36. {...this.state.allProps}
  37. id={this.state.id}
  38. key={this.state.id}
  39. />
  40. );
  41. }
  42. };
  43. }
  44. }