从内屏返回到父屏

时间:2018-10-11 19:21:25

标签: javascript reactjs react-native

我有一个像这样的堆栈导航器结构

index.js-> App.js-> LoginStck,HomeNavStack 从LoginStck成功登录后,我转到HomeNavStack 从此开始,我将堆栈重置为仅具有HomeNavStack(这样做是为了避免在返回时避免登录屏幕)在HomeStack中,有4个标签,即“首页/付款/配置文件/更多”,每个标签具有单独的堆栈。我导航到“更多”选项卡中的“更多屏幕”,并退出,我需要进入“登录堆栈”(第一个丢弃所有其他屏幕的屏幕)。

我确实尝试过这个`

dothis = async () => {
const someAction = StackActions.reset({
  index: 0,
  key: null,
  actions: [
    NavigationActions.navigate({ routeName: 'HomeNavigatorNew'})
  ]
});
this.props.navigation.navigate(someAction)

}

但是没有用。任何见解... 在此先感谢

`

1 个答案:

答案 0 :(得分:1)

建议在登录屏幕和其他屏幕之间进行切换的方法是使用createSwitchNavigator

有关更多信息,请检查:

https://reactnavigation.org/docs/en/switch-navigator.html#docsNav

我创建了一个简单的示例,可以为您的案例提供帮助:

import React from 'react';
import {
  View,
  Text,
  Button,
} from 'react-native';
import {
  createStackNavigator,
  createBottomTabNavigator,
  createSwitchNavigator,
} from 'react-navigation';

class LoginScreen extends React.Component {
  render() {
    return (
        <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
          <Text>LoginScreen</Text>
          <Button
              title={'Login'}
              onPress={() => this.props.navigation.navigate('HomeStack')}
          />
        </View>
    )
  }
}

class PaymentScreen extends React.Component {
  render() {
    return (
        <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
          <Text>PaymentScreen</Text>
        </View>
    )
  }
}

class ProfileScreen extends React.Component {
  render() {
    return (
        <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
          <Text>ProfileScreen</Text>
        </View>
    )
  }
}

class MoreScreen extends React.Component {
  render() {
    return (
        <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
          <Text>MoreScreen</Text>
          <Button
              title={'Logout'}
              onPress={() => this.props.navigation.navigate('LoginStack')}
          />
        </View>
    )
  }
}

const PaymentStack = createStackNavigator(
    {
      Payment: PaymentScreen,
    }
);

const ProfileStack = createStackNavigator(
    {
      Profile: ProfileScreen,
    }
);

const MoreStack = createStackNavigator(
    {
      More: MoreScreen,
    }
);

const HomeStack = createBottomTabNavigator(
    {
      PaymentStack: PaymentStack,
      ProfileStack: ProfileStack,
      MoreStack: MoreStack,
    }
);

const LoginStack = createStackNavigator(
    {
      Login: LoginScreen,
    }
);

export default createSwitchNavigator(
    {
      LoginStack: LoginStack,
      HomeStack: HomeStack,
    }
);