react-native-navigation的迁移库

ScreenRegistry.js 1.2KB

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