简体   繁体   English

切换导航器在 React Native 中不起作用

[英]Switch navigator does not works in React Native

I've asked a question already, and modified as much as I can, but still in trouble.我已经问了一个问题,并尽可能多地修改,但仍然遇到麻烦。 Combined all children Screen js files into App.js将所有子 Screen js 文件合并成 App.js

When I compile and run, App shows LoginScreen.js, not LoadingScreen.js.当我编译和运行时,App 显示 LoginScreen.js,而不是 LoadingScreen.js。

Also, onPress={this.props.navigation.navigate('Loading')} does not works.此外, onPress={this.props.navigation.navigate('Loading')} 不起作用。 If I press the button, it shows nothing.如果我按下按钮,它什么也不显示。

What am I still missing?我还缺少什么?

import React from 'react'
import { StyleSheet, Platform, Image, Text, View, TextInput, Button, ActivityIndicator } from 'react-native'
import { createAppContainer, createSwitchNavigator } from 'react-navigation'
import { createBottomTabNavigator } from 'react-navigation-tabs';
import Ionicons from 'react-native-vector-icons/Ionicons';

class LoadingScreen extends React.Component {

  render() {
    return (
      <View style={styles.loadingcontainer}>
        <Text>Loading</Text>
        <ActivityIndicator size="large" />
        <Button title="Move to LoginScreen" onPress={this.props.navigation.navigate('Login')} />
      </View>
    )
  }
}

class LoginScreen extends React.Component {
  state = { email: '', password: '' }

  render() {
    return (
      <View style={styles.logincontainer}>
        <Button
          title='Sign in' onPress={this.props.navigation.navigate('Loading')}
        />
        <Button
          title='Sign Up'
        />
      </View>
    )
  }
}

const styles = StyleSheet.create({
  loadingcontainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  logincontainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  logintextInput: {
    height: 40,
    width: '90%',
    borderColor: 'gray',
    borderWidth: 1,
    marginTop: 8
  },
})

const App = createSwitchNavigator(
  {
    Loading: {
      screen: LoadingScreen,
    },
    Login: {
      screen: LoginScreen,
    },
  }
);

export default createAppContainer(App);

Thank you for your help.感谢您的帮助。

For just navigation, you can use a stack navigator.对于仅导航,您可以使用堆栈导航器。 The switch navigator used for conditional rendering of two different stacks.切换导航器用于两个不同堆栈的条件渲染。 Anyways you can set loading as the first screen buy setting initialRouteName to loading.无论如何,您可以将加载设置为第一个屏幕购买将 initialRouteName 设置为加载。 Here is an example这是一个例子

 createSwitchNavigator(
  {
    LimitedAccess: {
     screen: Trial,
    },
    AppScreens: {
      screen: AppScreens,
    },

    AuthScreens: {
      screen: AuthScreens,
    },
  },
  {
    initialRouteName: signedIn ? 'AppScreens' : 'AuthScreens',
  },
),

); );

Note: Here signedIn is a conditional operator which decides the rendering of stacks.注意:这里的 signedIn 是一个条件运算符,它决定堆栈的呈现。

The way your props are currently defined causes them to be instantly executed.您的道具当前定义的方式会导致它们立即执行。

The onPress prop is instantly executed. onPress 道具会立即执行。

return (
  <View style={styles.loadingcontainer}>
    <Text>Loading</Text>
    <ActivityIndicator size="large" />
    <Button title="Move to LoginScreen" onPress={this.props.navigation.navigate('Login')} />
  </View>
)

You should instead attach a function to onPress that can be executed when the button is pressed.您应该将一个函数附加到 onPress ,该函数可以在按下按钮时执行。

return (
  <View style={styles.loadingcontainer}>
    <Text>Loading</Text>
    <ActivityIndicator size="large" />
    <Button title="Move to LoginScreen" onPress={() =>  this.props.navigation.navigate('Login')} />
  </View>
)

Your onPress-calls are running instantly, which causes your problems.您的 onPress-calls 正在立即运行,这会导致您的问题。 Change to:改成:

import React from 'react'
import { StyleSheet, Platform, Image, Text, View, TextInput, Button, ActivityIndicator } from 'react-native'
import { createAppContainer, createSwitchNavigator } from 'react-navigation'
import { createBottomTabNavigator } from 'react-navigation-tabs';
import Ionicons from 'react-native-vector-icons/Ionicons';

class LoadingScreen extends React.Component {

  render() {
    return (
      <View style={styles.loadingcontainer}>
        <Text>Loading</Text>
        <ActivityIndicator size="large" />
        <Button title="Move to LoginScreen" onPress={() => this.props.navigation.navigate('Login')} />
      </View>
    )
  }
}

class LoginScreen extends React.Component {
  state = { email: '', password: '' }

  render() {
    return (
      <View style={styles.logincontainer}>
        <Button
          title='Sign in' onPress={() => this.props.navigation.navigate('Loading')}
        />
        <Button
          title='Sign Up'
        />
      </View>
    )
  }
}

const styles = StyleSheet.create({
  loadingcontainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  logincontainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center'
  },
  logintextInput: {
    height: 40,
    width: '90%',
    borderColor: 'gray',
    borderWidth: 1,
    marginTop: 8
  },
})

const App = createSwitchNavigator(
  {
    Loading: {
      screen: LoadingScreen,
    },
    Login: {
      screen: LoginScreen,
    },
  }
);

export default createAppContainer(App);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM