react-native-navigation的迁移库

ContainerRegistry.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import React, {Component} from 'react';
  2. import {AppRegistry} from 'react-native';
  3. import * as PropsStore from './PropsStore';
  4. import * as ContainerStore from './ContainerStore';
  5. export function registerContainer(containerKey, getContainerFunc) {
  6. const OriginalContainer = getContainerFunc();
  7. const NavigationContainer = wrapContainer(containerKey, OriginalContainer);
  8. ContainerStore.setContainerClass(containerKey, NavigationContainer);
  9. AppRegistry.registerComponent(containerKey, () => NavigationContainer);
  10. }
  11. export const bla = {};
  12. function wrapContainer(containerKey, OriginalContainer) {
  13. return class extends Component {
  14. constructor(props) {
  15. super(props);
  16. if (!props.containerId) {
  17. throw new Error(`Container ${containerKey} does not have a containerId!`);
  18. }
  19. this.state = {
  20. containerId: props.containerId,
  21. allProps: {...props, ...PropsStore.getPropsForContainerId(props.containerId)}
  22. };
  23. }
  24. componentWillReceiveProps(nextProps) {
  25. this.setState({
  26. allProps: {...nextProps, ...PropsStore.getPropsForContainerId(this.state.containerId)}
  27. });
  28. }
  29. render() {
  30. return (
  31. <OriginalContainer
  32. ref={(r) => bla.ref = r}
  33. {...this.state.allProps}
  34. containerId={this.state.containerId}
  35. />
  36. );
  37. }
  38. };
  39. }