加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

React Navigation--Stack Navigator 详细的例子

发布时间:2020-12-15 05:09:09 所属栏目:百科 来源:网络整理
导读:/** * Created by YiBing on 2017/5/5. * * 一个StackNavigator详细的例子,效果:1个主页面,2个子页面,每个页面有三个按钮,可以跳转到相应的页面。 * * React Navigation -- StackNavigator: API Definition--StackNavigator(RouteConfigs,StackNavigato
/**
 * Created by YiBing on 2017/5/5.
 *
 * 一个StackNavigator详细的例子,效果:1个主页面,2个子页面,每个页面有三个按钮,可以跳转到相应的页面。
 *
 * React Navigation -- StackNavigator:
 API Definition--StackNavigator(RouteConfigs,StackNavigatorConfig);
 RouteConfigs--StackNavigator({

  // For each screen that you can navigate to,create a new entry like this:
  Profile: {

    // `ProfileScreen` is a React component that will be the main content of the
screen.
    screen: ProfileScreen,// When `ProfileScreen` is loaded by the StackNavigator,it will be given a
`navigation` prop.

    // Optional: When deep linking or using react-navigation in a web app,this path is
used:
    path: 'people/:username',// The action and route params are extracted from the path.

    // Optional: Override the `navigationOptions` for the screen
    navigationOptions: ({navigation}) => ({
      title: `${navigation.state.params.username}'s Profile'`,}),},...MyOtherRoutes,});

 StackNavigatorConfig--
 initialRouteName:默认路由,就是第一个要显示的页面。
 initialRouteParams;
 navigationOptions:对所有的screen的默认配置。
 paths:对所有路由的path属性的默认配置。
 mode:定义渲染风格,enum(card(IOS、Android都可以)、modal(仅IOS))。
 headerMode:enum(float、screen、none)。
 cardStyle:Use this prop to override or extend the default style for an individual
 card in stack.
 transitionConfig:Function to return an object that overrides default screen
 transitions.
 onTransitionStart:Function to be invoked when the card transition animation is about
 to start.
 onTransitionEnd:Function to be invoked once the card transition animation completes.


 Screen Navigation Options:
 title: Generic title that can be used as a fallback for headerTitle and tabBarLabel。
 headerRight:React Element to display on the right side of the header。
 header
 headerTitle
 headerBackTitle:Title string used by the back button on iOS or null to disable
 label. Defaults to scene title。
 headerTruncatedBackTitle
 headerLeft
 headerStyle
 headerTitleStyle
 headerBackTitleStyle
 headerTintColor
 headerPressColorAndroid:Color for material ripple (Android >= 5.0 only)
 gesturesEnabled


 Navigator Props:
 const SomeStack = StackNavigator({
    // config
  });
 <SomeStack
 screenProps={
     // this prop will get passed to the screen components asthis.props.screenProps
   } />
*/

import React from 'react';
import {
    Button,ScrollView,Text,} from 'react-native';
import {
    StackNavigator,} from 'react-navigation';

const MyNavScreen = ({navigation,banner}) => (
    <ScrollView>
        <Text>{banner}</Text>
        <Button
            onPress={() => navigation.navigate('Profile',{ name: 'Jane' })}
            title="Go to a profile screen"
        />
        <Button
            onPress={() => navigation.navigate('Photos',{ name: 'Jane' })}
            title="Go to a photos screen"
        />
        <Button
            onPress={() => navigation.goBack(null)}
            title="Go back"
        />
    </ScrollView>
);

const MyHomeScreen = ({navigation}) => (
    <MyNavScreen
        banner="Home Screen"
        navigation={navigation}
    />
);
MyHomeScreen.navigationOptions = {
    title: 'Welcome',};

const MyPhotosScreen = ({navigation}) => (
    <MyNavScreen
        banner={`${navigation.state.params.name}'s Photos`}
        navigation={navigation}
    />
);
MyPhotosScreen.navigationOptions = {
    title: 'Photos',};

const MyProfileScreen = ({navigation}) => (
    <MyNavScreen
        banner={
            `${navigation.state.params.mode === 'edit' ? 'Now Editing ' : ''}${navigation.state.params.name}'s Profile`
        }
        navigation={navigation}
    />
);

MyProfileScreen.navigationOptions = props => {
    const {navigation} = props;
    const {state,setParams} = navigation;
    const {params} = state;
    return {
        headerTitle: `${params.name}'s Profile!`,// Render a button on the right side of the header.
        // When pressed switches the screen to edit mode.
        headerRight: (
            <Button
                title={params.mode === 'edit' ? 'Done' : 'Edit'}
                onPress={() => setParams({ mode: params.mode === 'edit' ? '' : 'edit' })}
            />
        ),};
};

const SimpleStack = StackNavigator(
    {
        Profile: {
            path: 'people/:name',screen: MyProfileScreen,Photos: {
            path: 'photos/:name',screen: MyPhotosScreen,Home: {
            screen: MyHomeScreen,{
        initialRouteName: 'Home',}
);

export default SimpleStack;






(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读