如何通过Navigation.setRoot将导航器prop从react-native-navigation v2传递到我的初始屏幕

时间:2019-06-26 07:49:29

标签: react-native react-native-navigation wix-react-native-navigation react-native-navigation-v2

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

Navigation.startSingleScreenApp

Navigation.setRoot

当我从Navigation.startSingleScreenApp(v1)切换到Navigation.setRoot(v2)时,我不再拥有要在应用程序中导航的导航器道具。

enter image description here 我已经复制并粘贴了以下所有相关代码

RegisterScreens

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',
    },
  });
});

SplashScreen

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);

1 个答案:

答案 0 :(得分:0)

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

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

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