【问题标题】:React Native Navigating Between Two ScreensReact Native 在两个屏幕之间导航
【发布时间】:2019-01-05 12:16:40
【问题描述】:

在 React Native 中,我需要从一个屏幕导航到另一个屏幕。但我不明白这个概念。所以,我做了以下,

在 App.js 中

import React, {Component} from 'react';
import LinearGradient from 'react-native-linear-gradient';
import {StyleSheet, Text, Alert, View} from 'react-native';
import { Container, Header, Content, Button} from 'native-base';
import { createStackNavigator, createAppContainer } from 'react-navigation';


type Props = {};
export default class App extends Component<Props> {
ShowAlertDialog = () =>{

Alert.alert(

  // This is Alert Dialog Title
  'Lopels',

  // This is Alert Dialog Message. 
  'Welcome to Lopels',
  [
    // First Text Button in Alert Dialog.
    {text: 'Ask me later', onPress: () => console.log('Ask me later Button Clicked')},

    // Second Cancel Button in Alert Dialog.
    {text: 'Cancel', onPress: () => console.log('Cancel Button Pressed'), style: 'cancel'},

    // Third OK Button in Alert Dialog
    {text: 'OK', onPress: () => console.log('OK ButtonPressed')},

  ]

)

}
render() {
return (
  <Container>
  <LinearGradient colors={['#007991', '#78ffd6']} style={styles.linearGradient}>
    <Text style={styles.logoText}>
      Lopels
    </Text>
    <Text style={styles.tagText}>
      The Complete Loyalty Platform
    </Text>
    <Button style={styles.button} onPress={this.ShowAlertDialog}>
        <Text style={styles.buttonText}>Get Started</Text>
    </Button>
  </LinearGradient>
  </Container>
);
}
}

const styles = StyleSheet.create({
linearGradient: {
flex: 1,
paddingLeft: 5,
paddingRight: 5,
},

  logoText: {
    fontSize: 50,
    fontFamily: 'Graceland',
    textAlign: 'center',
    marginTop: 170,
    color: '#ffffff',
    backgroundColor: 'transparent',
  },
  tagText:{
    fontSize:15,
    letterSpacing: 3,
    fontFamily:'Actor',
    textAlign: 'center',
    color: 'black',
    backgroundColor: 'transparent',
  },
  button:{
    marginLeft: '15%',
    marginTop: '8%',
    width:250,
    borderRadius: 10,
    backgroundColor:'#ffffff',
    borderColor: "transparent",
  },
  buttonText:{
    marginTop: '8%',
    marginLeft: '35%',
    fontFamily: 'Actor',
    color: '#282828',
    fontSize: 18,
  }
});

其他屏幕的文件夹在文件夹./src/Login Screen/index.js中

单击 App.js 中的“开始”按钮后,我需要导航到登录屏幕

【问题讨论】:

    标签: reactjs react-native navigation react-navigation


    【解决方案1】:

    应用的入口是App.js,导入到index.ios.js

    index.ios.js

    import React, { Component } from 'react';
    import {
      AppRegistry
    } from 'react-native';
    
    import App from './component/stackScreens/App';
    
    AppRegistry.registerComponent('reactNav', () => App);
    

    在 App.js 中,StackNavigator 被导入。 App.js 是一个堆栈,其中包含导航所需的所有组件。

    App.js

    import React from 'react';
    
    import { StackNavigator } from 'react-navigation';
    import ScreenOne from './ScreenOne';
    import ScreenTwo from './ScreenTwo';
    import ScreenThree from './ScreenThree';
    
    const App = StackNavigator({
        ScreenOne: { screen: ScreenOne},
        ScreenTwo: { screen: ScreenTwo},
        ScreenThree: {screen: ScreenThree}
    })
    
    export default App;
    

    ScreenOne.js

    class ScreenOne extends Component {
      render() {
        const { navigate } = this.props.navigation
        return (
          <View style={styles.container}>
            <TouchableHighlight
              onPress={() => navigate("ScreenTwo")}
              style={styles.button}>
              <Text
                style={styles.buttonText}>Screen One </Text>
            </TouchableHighlight>
          </View>
        );
      }
    };
    

    const { navigate } = this.props.navigation:指定屏幕到 导航。

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2018-07-14
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多