亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

【React Native 極速指南】進階篇

標簽:
React Native

这篇文章你将会学习到:

  • 如何安装路由 react-navigation

  • 如何使用路由

    • 创建 StackNavigator

    • 页面间的转场和传递参数

    • 相关配置

    • TabNavigator

  • 其他通用组件

如何安装路由 react-navigation

  • yarn add react-navigation

  • Or npm install --save react-navigation

如何使用路由

创建 StackNavigator

  • 创建 Screen(View) 组件: Home

  • 创建 StackNavigator: NavigationContainer

    • 导入 createStackNavigator

    • 创建 NavigationRouteConfigMap

    • 创建 StackNavigatorConfig

  • 创建 App(React.Component)

    • 引用 Navigator

    • 渲染 StackNavigator

// home.jsimport React from 'react';import { View, Text } from 'react-native';export default class HomeScreen extends React.Component {
    render() {        return (            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Text>Home Screen</Text>
            </View>
        );
    }
}
// navigator.jsimport React from 'react';import { createStackNavigator } from 'react-navigation';import Home from './home';const NavigationRouteConfigMap = {    Home: {        screen: Home,        navigationOptions: {            header: null,            gesturesEnabled: false,
        },
    },
};const StackNavigatorConfig = {    initialRouteName: 'Home',
};export default createStackNavigator(NavigationRouteConfigMap, StackNavigatorConfig);
// app.jsimport React, { PureComponent } from 'react';import AppNavigator from './navigator';export default class App extends PureComponent {
    render() {        return <AppNavigator />;
    }
}
// index.jsimport {AppRegistry} from 'react-native';import App from './src/app';import {name as appName} from './app.json';

AppRegistry.registerComponent(appName, () => App);

页面间的转场和传递参数

  • 创建 Screen(View) 组件: Demo

  • 在页面上增加 Navigator

    • this.props.navigation.navigate('Demo', { test: 'hello'})

    • this.props.navigation.state.params

// demo.jsimport React from 'react';import { View, Text } from 'react-native';export default class DemoScreen extends React.Component {
    render() {        return (            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Text>Demo Screen</Text>
            </View>
        );
    }
}
// navigator.js//...const NavigationRouteConfigMap = {    //...
    Demo: {
        screen: Demo,
        navigationOptions: {
            title: 'Demo'
        },
    },
};//...
// home.jsimport React from 'react';import { View, Text, Button } from 'react-native';export default class HomeScreen extends React.Component {
    render() {        return (            <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                <Text>Home Screen</Text>
                <Button 
                    title="Go to Demo"
                    onPress={() => this.props.navigation.navigate('Demo', { test: 'hello' })}
                >                </Button>
            </View>
        );
    }
}

相关配置

TODO

TabNavigator

  • StackNavigatorConfig

    • headerTitleAllowFontScaling

    • headerStyle

    • headerLeft

    • headerRight

    • headerMode

    • initialRouteName

    • mode

    • cardStyle

    • navigationOptions

  • NavigationRouteConfigMap

    • title

    • left

    • right

    • screen

    • navigationOptions

TabNavigator

  • 创建 TabNavigator: NavigationContainer

    • 创建 NavigationRouteConfigMap

    • 创建 BottomTabNavigatorConfig

  • 在 StackNavigator 添加 TabNavigator

// tab.jsimport React from 'react';import { createBottomTabNavigator } from 'react-navigation';import Home from './home';import Demo2 from './demo2';const NavigatorTab = {    Home: {        screen: Home,        navigationOptions: {            tabBarLabel: 'Home',            showIcon: true,
        },
    },    Demo2: {        screen: Demo2,        navigationOptions: {            tabBarLabel: 'Demo',            showIcon: true,
        },
    },
};const TabOptions = {    animationEnabled: true,
};export default createBottomTabNavigator(NavigatorTab, TabOptions);
// navigator.js// ...const NavigationRouteConfigMap = {    // ...
    Tab: {
        screen: Tab,
        navigationOptions: {
            header: null,
            gesturesEnabled: false,
        },
    }
};const StackNavigatorConfig = {
    initialRouteName: 'Tab',
};export default createStackNavigator(NavigationRouteConfigMap, StackNavigatorConfig);

其他通用组件

  • 内置组件

    • Text, Button, View ...

    • StatusBar

    • RefreshControl

    • FlatList

  • 第三方组件

    • react-native-i18n

    • react-native-qrcode

    • react-native-splash-screen

    • react-native-svg-icon

    • react-native-svg

    • react-native-vector-icons

    • react-native-device-info

    • react-native-camera

    • react-native-swiper

    • react-native-root-toast

    • awesome-react-native





點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
Web前端工程師
手記
粉絲
12
獲贊與收藏
135

關注作者,訂閱最新文章

閱讀免費教程

  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消