0 navigation reactjs react-native react-navigation
在 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,
}
});
Run Code Online (Sandbox Code Playgroud)
而文件夹其他屏幕在文件夹 ./src/Login Screen/index.js 中
单击 App.js 中的“入门”按钮后,我需要导航到登录屏幕
应用入口为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);
Run Code Online (Sandbox Code Playgroud)
在 App.js 中,引入了 StackNavigator。App.js 是一个包含导航所需的所有组件的堆栈。
应用程序.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;
Run Code Online (Sandbox Code Playgroud)
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>
);
}
};
Run Code Online (Sandbox Code Playgroud)
const { navigate } = this.props.navigation:指定要导航的屏幕。
| 归档时间: |
|
| 查看次数: |
2497 次 |
| 最近记录: |