【问题标题】:React Native - StackActions.reset does nothingReact Native - StackActions.reset 什么都不做
【发布时间】:2019-02-18 15:01:19
【问题描述】:

我有这个函数,当按下<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 的一部分,它是主要App 导航器的子导航器。我正在尝试从Home 路由执行StackActions.reset,它是根App 导航器的一部分。

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

更新

我试过了:

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

第一个Navigation.navigate 有效,但sub-action 导航到Rentable 无效。

更新

我仍在寻找 StackActions.reset 为什么不起作用的答案 - 但对于我正在尝试做的事情(从父导航器导航到子导航器的页面)...我找到了解决方法/ @kashishgrover 在这里提出的方法:https://github.com/react-navigation/react-navigation/issues/2398

但这不是一个完美的解决方法 - 它强制应用程序转到中间页面,然后侦听一个参数,因此在瞬间 - 中间页面加载,然后加载预期页面。

【问题讨论】:

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


    【解决方案1】:
    this.props.navigation.dispatch(StackActions.reset({
        index: 0,
        key: null,
        actions: [NavigationActions.navigate({ routeName: 'OpenCamera' })],
    }));
    

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

    【讨论】:

    • 这是唯一对我有用的解决方案来重置堆栈并防止返回操作。谢谢。
    • @MaykedaRosaHerbst yw,很高兴我为互联网提供了有价值的服务!
    【解决方案2】:

    在我使用的堆栈之间导航:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 2020-05-23
      • 1970-01-01
      • 2019-03-08
      • 2016-12-24
      • 2017-04-18
      • 2011-08-03
      相关资源
      最近更新 更多