如何在React-native中从一个抽屉屏幕切换到一个非抽屉屏幕?

时间:2019-04-04 11:35:31

标签: javascript react-native

我想知道一种从一个屏幕切换到另一个屏幕的简便方法。因为我真的对这个问题感到沮丧。我在HomeScreen中声明了一个堆栈导航器,以初始化启动屏幕。这是该代码-

HomeScreen.js

import React from 'react';
import { StyleSheet, Text, View, AsyncStorage } from 'react-native';
import {StackNavigator} from 'react-navigation';
import WelcomeScreen from './WelcomeScreen';
import LoginScreen from './components/LoginScreen';
import NoteMeHome from './components/NoteMeHome';
import HomeDrawer from './HomeDrawer/HomeDrawer';
import SettingsScreen from './components/SettingsScreen';
import TestScreen from './components/TestScreen';

class HomeScreen extends React.Component {

  state = {
    getValue: null,

  }

  async componentDidMount() {

    const token = await AsyncStorage.getItem('toke');
    this.setState({ getValue: token });

  }

  render() {
    console.log('#ZZZ:', this.state.getValue);

    if(this.state.getValue !== null) {
      return (
        <AppStackNavigator/>
      );
    } else {
      return (
        <AppStackNavigator2/>
      );
    }

  }
}

const AppStackNavigator = new StackNavigator({
  HomeDrawer: {screen:HomeDrawer},
  WelcomeScreen: {screen:WelcomeScreen},

  LoginScreen: {screen:LoginScreen},
  NoteMeHome: {screen:NoteMeHome},

  SettingsScreen: {screen:SettingsScreen},
  TestScreen: {screen:TestScreen}

})

const AppStackNavigator2 = new StackNavigator({
    LoginScreen: {screen:LoginScreen},
  WelcomeScreen: {screen:WelcomeScreen},
  HomeDrawer: {screen:HomeDrawer
  },

  NoteMeHome: {screen:NoteMeHome},

  SettingsScreen: {screen:SettingsScreen},
  TestScreen: {screen:TestScreen}

})

export default HomeScreen;

登录后,在下一个屏幕中,我用Drawer导航创建了一个名为HomeDrawer的类。这是该代码-

HomeDrawer.js

import React, {Component} from "react";
import {View, Text, StyleSheet, ScrollView, Image, AsyncStorage, ImageBackground} from 'react-native';
import {Container, Content, Icon, Header, Body} from 'native-base';
import {DrawerNavigator, StackNavigator, DrawerItems, SafeAreaView} from 'react-navigation';

import NoteMeHome from '../components/NoteMeHome';
import SettingsScreen from '../components/SettingsScreen';

import {Root} from 'native-base';
import {Font, AppLoading} from 'expo';
import WelcomeScreen from "../WelcomeScreen";

let user_email ='', user_first_name='';

class HomeDrawer extends Component {

  state = {

    loading: true
  }

  static navigationOptions = {
    headerLeft: null
}

  componentDidMount() {
    AsyncStorage.getItem("user_email").then(value => {
      console.log(value);
      // you will need to handle case when `@ProductTour:key` is not exists
     user_email = value;
    });

    AsyncStorage.getItem("user_first_name").then(value => {
      console.log(value);
      // you will need to handle case when `@ProductTour:key` is not exists
     user_first_name = value;
    });

  }

  async componentWillMount() {
    await Font.loadAsync ({
      Roboto: require('native-base/Fonts/Roboto.ttf'),
      Roboto_medium: require('native-base/Fonts/Roboto_medium.ttf')
    });
    this.setState({loading:false});
  }

  render() {
    if(this.state.loading) {
      return(
        <Root>
          <AppLoading/>
        </Root>
      )
    }
    return(
      <MyApp/>
    )

  }
}

const CustomDrawerContentComponent = (props) => (

 <View style={{backgroundColor:"#ffff", height:'100%'}}>

      <ImageBackground source={require('../assets/header.jpeg')} style={{width: '100%', height: 150,  resizeMode:'cover', backgroundColor:"@aaaa"}}>  
      <Body style={styles.drawerBody}>
        <Image
          style={styles.drawerImage}
          source={{uri: 'https://img.icons8.com/ultraviolet/80/000000/administrator-male.png'}}
        />
        <View style={styles.drawerHeaderText}>
          <Text style={{color:'#ffffff', fontSize:20, fontWeight:'400'}}>{user_email}</Text>
          <Text style={{color:'#ffffff', fontSize:16, fontWeight:'200'}}>{user_first_name}</Text>
        </View>

      </Body>


      </ImageBackground>


    <Content style={{marginTop:30 }}>
      <DrawerItems {...props}/>
    </Content>

  </View>



);

const MyApp = DrawerNavigator({
  NoteMeHome:{
    screen: NoteMeHome
  },
  Settings:{
    screen: SettingsScreen
  },
  WelcomeScreen : {
    screen: WelcomeScreen
  }


},

{
  initialRouteName: 'NoteMeHome',
  drawerPosition: 'left',
  contentComponent: CustomDrawerContentComponent,
  drawerOpenRoute: 'DrawerOpen',
  drawerCloseRoute: 'DrawerClose',
  drawerToggleRoute: 'DrawerToggle'
}


);

export default HomeDrawer;

我已经为Drawer导航器创建了SettingsScreen.js。这是该代码-

SettingsScreen.js

import React,{Component} from 'react';
import {View, Text, StyleSheet, Image, TouchableHighlight} from 'react-native';
import {Container, Content, Icon, Button} from 'native-base';
import CustomHeader from './CustomHeader';

class SettingsScreen extends Component {
  static navigationOptions = ({navigation}) => ({
    title: "Settings",
    headerLeft: <Icon name="ios-menu" style={{paddingLeft:10}}
    onPress={()=>navigation.navigate('DrawerOpen')}/>,

    drawerIcon: 

    <Image source={{uri: 'https://img.icons8.com/ultraviolet/40/000000/automatic.png'}}
            style={styles.icon}
    />
  })

  render() {


    return(


      <Container>
        <CustomHeader
          title="Settings"
          drawerOpen={()=>this.props.navigation.navigate('DrawerOpen')}
        />
        <Content contentContainerStyle={{flex:1, alignItems:'center', 
        justifyContent:'center', padding:10}}>
        <Button full onPress={() => this.props.navigation.navigate('TestScreen')}>
          <Text style={{color:'white'}}>Go To Test Screen</Text>
        </Button>
        </Content>
      </Container>
    )
  }

}

export default SettingsScreen;

现在,问题是- 您可以在我的SettingsScreen.js文件中看到,我在按下按钮时添加了此行-

this.props.navigation.navigate('TestScreen')

哪个应启动抽屉导航之外的TestScreen类。这是该代码-

TestScreen.js

import React,{Component} from 'react';
import {View, Text, StyleSheet, Image, TouchableHighlight} from 'react-native';

class TestScreen extends Component {
render() {

    return(
        <View>
            <Text>Hello from Test Screen</Text>
        </View>
    )
  }

}

export default TestScreen;

但是,单击SettingsScreen.js类中的按钮后,没有任何作用。我可以通过更改代码onPress来访问HomeDrawer中的另一个类。但是,如果其他任何类不在抽屉式导航中,则它将无法正常工作。

我用于反应导航的版本是-

  

react-navigation@1.0.0-beta.23

我不想更新此版本,因为在最新版本中,我的代码显示了其他一些错误。因此,请回答以下问题:是否有人可以在代码的当前配置方面为我提供帮助。

1 个答案:

答案 0 :(得分:0)

更新 请使用npm -i react-navigation@latest

更新反应导航

,在使用代码之前,您还需要参考以下链接。更新后,我们需要注意很多事情。

链接:https://reactnavigation.org/blog/2018/11/17/react-navigation-3.0.html

原始答案

您可以使用开关导航器。

首先,您必须制作一个导航课,

例如:

import {
  createAppContainer,
  createStackNavigator,
  createDrawerNavigator,
  createSwitchNavigator
} from "react-navigation";

...
...
...

const AppStackNavigator = createStackNavigator({
  HomeDrawer: {screen:HomeDrawer},
  WelcomeScreen: {screen:WelcomeScreen},
  LoginScreen: {screen:LoginScreen},
  NoteMeHome: {screen:NoteMeHome},
  SettingsScreen: {screen:SettingsScreen},
  TestScreen: {screen:TestScreen}
})

const AppStackNavigator2 = createStackNavigator({
  LoginScreen: {screen:LoginScreen},
  WelcomeScreen: {screen:WelcomeScreen},
  HomeDrawer: {screen:HomeDrawer},
  NoteMeHome: {screen:NoteMeHome},
  SettingsScreen: {screen:SettingsScreen},
  TestScreen: {screen:TestScreen}
})

const DrawerNavi = createDrawerNavigator({
  NoteMeHome:{ screen: NoteMeHome},
  Settings:{ screen: SettingsScreen},
  WelcomeScreen : { screen: WelcomeScreen}
},
{
  initialRouteName: 'NoteMeHome',
  drawerPosition: 'left',
  contentComponent: CustomDrawerContentComponent,
  drawerOpenRoute: 'DrawerOpen',
  drawerCloseRoute: 'DrawerClose',
  drawerToggleRoute: 'DrawerToggle'
}
);

const switchNavigator = createSwitchNavigator(
  {
    AppStackNavigator: AppStackNavigator,
    AppStackNavigator2: AppStackNavigator2,
    DrawerNavi: DrawerNavi
  },
  { headerMode: "none", initialRouteName: "AppStackNavigator" }
);

const App = createAppContainer(switchNavigator);

export default App;

然后您可以使用来切换这些导航,

this.props.navigation.navigate("AppStackNavigator2");