React Native-StackActions.reset什么都不做

时间:2018-09-13 20:09:24

标签: react-native navigation components react-navigation stack-navigator

我具有按下<TouchableWithoutFeedback>区域时执行的功能:

navigateToRentable = () => {
    console.log('in navigateToRentable');

    this.props
     .navigation
     .dispatch(StackActions.reset({
       index: 0,
       actions: [
         NavigationActions.navigate({
           routeName: 'Rent',
           actions: [
              NavigationActions.navigate({
               routeName: 'Rentable',
             }),
           ]
         }),
       ],
     }))
}

我在控制台中看到in navigateToRentable,所以我知道该方法正在触发,但是什么也没发生-控制台中没有其他输出。

这是我的导航的结构:

CameraNavigator.js

import React from 'react';
import { StyleSheet, Text, View, Alert, Permissions, Linking, TouchableOpacity, Platform, ImageStore, Dimensions } from 'react-native';
import { createStackNavigator } from 'react-navigation';

import RentScreenNavigator from './RentScreenNavigator';
import RentScreen from '../pages/RentScreen';
import CameraScreen from '../pages/CameraScreen';
import RentableScreen from '../pages/RentableScreen';

export default CameraNavigator = createStackNavigator(
  {
    OpenCamera: {
      screen: CameraScreen,
      navigationOptions: ({ navigation }) => ({
        header: null
      }),
    },
    RentDetails: {
      screen: RentScreen,
      navigationOptions: ({ navigation }) => ({
        header: null
      }),
    },
    Rentable: {
      screen: RentableScreen,
      navigationOptions: ({ navigation }) => ({
        header: null
      }),
    }
  },
  {
    initialRouteName: 'Rent',
  },
);

CameraScreenNavigator.js

import React from 'react';
import { StyleSheet, Text, View, Alert, Permissions, Linking, TouchableOpacity, Platform, ImageStore, Dimensions } from 'react-native';
import { createStackNavigator } from 'react-navigation';

import CameraNavigator from './CameraNavigator';

export default class CameraScreenNavigator extends React.Component {
  constructor(props) {
    super(props)
  }

  render() {
    return (
      <CameraNavigator/>
    )
  }
};

App.js

import React from 'react';
import { StyleSheet, Platform, Image, Text, View, ScrollView } from 'react-native';
import Ionicons from 'react-native-vector-icons/Ionicons';
import { Icon } from 'react-native-elements';
import { createBottomTabNavigator } from 'react-navigation';
import firebase from 'react-native-firebase';
import { YellowBox } from 'react-native';

import HomeScreen from './pages/HomeScreen';
import ProfileScreen from './pages/ProfileScreen';
//import CameraScreen from './pages/CameraScreen';
import CameraScreenNavigator from './components/CameraScreenNavigator';
//import RentScreenNavigator from './components/RentScreenNavigator';

YellowBox.ignoreWarnings(['Class RCTCxxModule']);
YellowBox.ignoreWarnings(['Warning: isMounted(...) is deprecated', 'Module RCTImageLoader']);
YellowBox.ignoreWarnings(['You should only render one navigator explicitly in your app, and other navigators should by rendered by including them in that navigator.']);

const AppNavigator = createBottomTabNavigator(
  {
    Home: HomeScreen,
    Profile: ProfileScreen,
    Rent: CameraScreenNavigator,
  },
  {
    navigationOptions: ({ navigation }) => ({
      tabBarIcon: ({ focused, tintColor }) => {
        const { routeName } = navigation.state;
        let iconName;
        if (routeName === 'Home') {
          iconName = 'home';
        } else if (routeName === 'Profile') {
          iconName = 'user';
        } else if (routeName === 'Rent') {
          iconName = 'plus-square';
        }

        // You can return any component that you like here! We usually use an
        // icon component from react-native-vector-icons
        return (
          <Icon
            name={iconName}
            color={tintColor}
            type='feather'
          />
        )
      }
    }),
    tabBarOptions: {
      activeTintColor: '#fff',
      inactiveTintColor: '#c9fffd',
      activeBackgroundColor: '#6de3dc',
      inactiveBackgroundColor: '#6de3dc'
    },
  },
);

export default class App extends React.Component {
  render() {
    return (
      <AppNavigator/>
    )
  }
}

您可能从StackActions.reset得知,我正在尝试导航到Rentable一部分的CameraNavigator路线,这是主要的{{1 }}导航器。我正在尝试从App路由执行StackActions.reset,该路由是根Home导航器的一部分。

总而言之,当我将应该App的区域按到navigate路线时,什么也没有发生。

更新

我尝试过:

Rentable

第一个this.props .navigation .dispatch( NavigationActions.navigate({ routeName: 'Rent', actions: [ NavigationActions.navigate({ routeName: 'Rentable', }), ] }), ) 有效,但是Navigation.navigatesub-action的导航无效。

更新

我仍在寻找为什么StackActions.reset无法正常工作的答案-但是对于我想做的事情(从父导航器导航到子导航器的页面)...我找到了一种解决方法/ @kashishgrover在此处提出的方法:https://github.com/react-navigation/react-navigation/issues/2398

但是,这不是一个完美的解决方法-它迫使应用程序转到中间页面,然后侦听参数,因此要紧接着-加载中间页面,然后再加载目标页面。< / p>

2 个答案:

答案 0 :(得分:1)

this.props.navigation.dispatch(StackActions.reset({
    index: 0,
    key: null,
    actions: [NavigationActions.navigate({ routeName: 'OpenCamera' })],
}));

我需要indexkeyrouteNameactions的正确组合。我也不需要一直导航到Home页面,只需导航到Rent页面,并且与主App导航器相交。

答案 1 :(得分:0)

我要在堆栈之间导航:

    const navigateToScreen = routeName => {
        navigation.dispatch(StackActions.reset(StackActions.popToTop()))   
        navigation.dispatch(NavigationActions.navigate({ routeName })) 
    }