安装 react-navigation
npm install --save react-navigation
代码:
import React from 'react'; import { StyleSheet, Text, View, Image } from 'react-native'; import { StackNavigator } from 'react-navigation'; class HomeScreen extends React.Component { static navigationOptions = { title: 'Welcome', }; render() { return <Text>Hello, Navigation!</Text>; } } export default App = StackNavigator({ Home: { screen: HomeScreen }, })
效果:
有两个页面的导航代码:
import React from 'react'; import { StyleSheet, Text, View, Image, Button } from 'react-native'; import { StackNavigator } from 'react-navigation'; class HomeScreen extends React.Component { static navigationOptions = { title: 'Welcome', }; render() { const { navigate } = this.props.navigation; return ( <View> <Text>Hello, Chat App!</Text> <Button onPress={() => navigate('Chat')} title="Chat with Lucy" /> </View> ); } } class ChatScreen extends React.Component { static navigationOptions = { title: 'Chat with Lucy', }; render() { return ( <View> <Text>Chat with Lucy</Text> </View> ); } } export default App = StackNavigator({ Home: { screen: HomeScreen }, Chat: { screen: ChatScreen }, })
点击Chat with Lucy就跳到另外一个窗口了。
参考:
https://reactnavigation.org/docs/intro/