react-native-navigation的迁移库

ContainerRegistry.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839
  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.saveContainerClass(containerKey, NavigationContainer);
  9. AppRegistry.registerComponent(containerKey, () => NavigationContainer);
  10. }
  11. function wrapContainer(containerKey, OriginalContainer) {
  12. return class extends Component {
  13. constructor(props) {
  14. super(props);
  15. if (!props.screenId) {
  16. throw new Error(`Screen ${containerKey} does not have a screenId!`);
  17. }
  18. this.state = {
  19. screenId: props.screenId,
  20. allProps: {...props, ...PropsStore.getPropsForScreenId(props.screenId)}
  21. };
  22. }
  23. componentWillReceiveProps(nextProps) {
  24. this.setState({
  25. allProps: {...nextProps, ...PropsStore.getPropsForScreenId(this.state.screenId)}
  26. });
  27. }
  28. render() {
  29. return (
  30. <OriginalContainer {...this.state.allProps} screenId={this.state.screenId}/>
  31. );
  32. }
  33. };
  34. }