0

我正在尝试从 react-native-navigation v1 迁移到 react-native-navigation v2。我正在努力从

Navigation.startSingleScreenApp

Navigation.setRoot

当我从 Navigation.startSingleScreenApp (v1) 切换到 Navigation.setRoot (v2) 时,我不再拥有导航应用程序所依赖的 navigator 道具。

在此处输入图像描述 我在下面复制并粘贴了所有相关代码

注册屏幕

import { Navigation } from 'react-native-navigation';
import SplashScreenScreen from './components/SplashScreen';
import { Provider } from 'react-redux';
import React from "react";
import SCREEN from './screenNames';

export default function registerScreens(store) {
  Navigation.registerComponent(
    SCREEN.SPLASH_SCREEN,
    () => props => (<Provider store={store}><SplashScreenScreen {...props} /></Provider>), () => SplashScreenScreen);

应用程序

import { Platform } from 'react-native';
import { Navigation } from 'react-native-navigation';
import registerScreens from './registerScreens';
import { Colors, Fonts } from './themes';
import { store } from './configureStore';
import NavigationListener from './NavigationEventListener';
import configureNotification from './configureNotification';

import SCREEN from './screenNames';
import Reactotron from 'reactotron-react-native';

const navBarTranslucent = Platform.OS === 'ios';

configureNotification();

registerScreens(store);

new NavigationListener(store);

const STARTING_SCREEN = SCREEN.SPLASH_SCREEN;

Navigation.events().registerAppLaunchedListener(() => {
  Reactotron.log('5');
  Navigation.setRoot({
    root: {
      stack: {
        children: [{
          component: {
            id: STARTING_SCREEN,
            name: STARTING_SCREEN
          }
        }],
      }
    },
    layout: {
      orientation: 'portrait',
    },
  });
});

闪屏

import React from 'react';
import { View, StyleSheet, Text } from 'react-native';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { PersistGate } from 'redux-persist/es/integration/react';
import { navigateToFirstScreen } from '../redux/splash';
import { Colors, Fonts, Metrics } from '../themes';
import { persistor } from '../configureStore';

export class SplashScreen extends React.Component {
  navigateTo = (screen) =>
    this.props.navigator.push({
      screen,
      overrideBackPress: true,
      backButtonHidden: true,
      animated: false,
      navigatorStyle: {
        disabledBackGesture: true,
      },
    });

  render() {
    const { dispatchNavigateToFirstScreen } = this.props;
    return (
      <PersistGate
        persistor={persistor}
        onBeforeLift={() => setTimeout(() => dispatchNavigateToFirstScreen(this.navigateTo), 2000)}><View style={styles.bodyContainer}
        >
          <Text>Jono</Text>
        </View>
      </PersistGate>
    );
  }
}

const styles = StyleSheet.create({
  bodyContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: Colors.splashScreen,
  },
  appTitleText: {
    fontSize: Fonts.size.splashScreenTitle,
    fontFamily: Fonts.type.extraBold,
    lineHeight: Metrics.lineHeight.appTitle,
    textAlign: 'center',
    color: Colors.textLightColor,
  },
});

SplashScreen.propTypes = {
  navigator: PropTypes.shape({
    push: PropTypes.func.isRequired,
  }).isRequired,
  dispatchNavigateToFirstScreen: PropTypes.func.isRequired,
};

const mapDispatchToProps = (dispatch) => {
  return {
    dispatchNavigateToFirstScreen: (navigateTo) =>
      dispatch(navigateToFirstScreen(navigateTo)),
  };
};

export default connect(null, mapDispatchToProps)(SplashScreen);
4

1 回答 1

1

我花了几个小时试图解决这个问题,所以我将发布我的结论作为答案。

this.props.navigator is not used anymore in 2.x.
You need to use Navigation

这个家伙有同样的问题,得出了同样的结论:https ://github.com/wix/react-native-navigation/issues/3795

于 2019-06-26T08:01:50.347 回答