1. 官网:
https://reactnavigation.org/docs/getting-started
2. 安装:
yarn add @react-navigation/native @react-navigation/stack
yarn add react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
3. 基础示例
import 'react-native-gesture-handler';
import * as React from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { Text, View, Button } from 'react-native'
import { createStackNavigator } from '@react-navigation/stack'
class Main extends React.Component {
render() {
return (
<View>
<Text>Main</Text>
<Button title="go to Other" onPress={this._nav } />
</View>
)
}
_nav=()=>{
let params={name:'john'}
this.props.navigation.navigate("Other",params)
}
}
class Other extends React.Component {
render() {
return (
<Text>Other,hello: {this.props.route.params.name}</Text>
)
}
}
const Stack = createStackNavigator()
export default class App extends React.Component {
render() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Main">
<Stack.Screen name="Main" component={Main}
options={{
headerRight: () => {
return <Button title="ok" onPress={this._okHandler} />
}
}}
/>
<Stack.Screen name="Other" component={Other} />
</Stack.Navigator>
</NavigationContainer>
);
}
_okHandler = () => {
alert('dfdsf')
}
}
网友评论