美文网首页
rn-navigation记录:

rn-navigation记录:

作者: JohnYuCN | 来源:发表于2020-05-12 12:00 被阅读0次

    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')
        }
    }
    
    

    相关文章

      网友评论

          本文标题:rn-navigation记录:

          本文链接:https://www.haomeiwen.com/subject/otcbnhtx.html