1. 程式人生 > >React Native 之 元件化開發

React Native 之 元件化開發

前言

  • 學習本系列內容需要具備一定 HTML 開發基礎,沒有基礎的朋友可以先轉至 HTML快速入門(一) 學習

  • 本人接觸 React Native 時間並不是特別長,所以對其中的內容和性質瞭解可能會有所偏差,在學習中如果有錯會及時修改內容,也歡迎萬能的朋友們批評指出,謝謝

  • 文章第一版出自簡書,如果出現圖片或頁面顯示問題,煩請轉至 簡書 檢視 也希望喜歡的朋友可以點贊,謝謝

React Native元件化介紹

  • React Native的核心思想就是元件化,相當於MVC的view,因此開發應用的最佳方式就是將功能元件化
  • 元件化最大的優點可以使Android和iOS能夠很方便地用很少地程式碼使用同一套元件,增加程式碼的複用性
  • React Native的元件化很簡單,基本步驟如下

    • 引用需要的庫


        // 引用
        import React, { Component } from 'react';
        import {
            需要用到的元件庫
        } from 'react-native';
    
    • 例項化檢視入口


        // 例項化檢視入口
        // 因為現在還是在想ES6轉化過程中,為了更好的相容性,這邊使用的是ES5的格式
        var 元件名 = React.createClass({
            render(){
                return(
                    需要例項化的檢視
                );
            }
        });
    
    • 檢視樣式入口


        // 檢視樣式入口
        // 因為現在還是在想ES6轉化過程中,為了更好的相容性,這邊使用的是ES5的格式
        var styles = StyleSheet.create({
            相應檢視的樣式
        });
    
    • 註冊並輸出元件


        module.exports = 元件名;
    
  • 生成元件後就可以在ios和android中使用生成後的元件了

    • 引入元件庫


        // 引入元件庫
        var 自定義元件名 = require('./元件名');
    
    • 使用生成的元件


        export default class TestRN2
    extends Component {
    render() { return ( <自定義元件名></自定義元件名> ); } }
  • 到此,元件化就簡單介紹到這,下面用一個綜合的例項來更好地幫助理解

React Native元件化綜合例項

  • 這邊我們通過完成微信的登入介面這個例項來詳細講元件化的使用,先來看看微信登入介面長啥樣

微信登入介面.png

  • 先來建立JS檔案,初始化一下格式並在index.ios.js中使用這個元件
    建立元件JS檔案.gif

    • 初始化JS格式


        import React, { Component } from 'react';
            import {
                AppRegistry,
                StyleSheet,
                Text,
                View,
                Image,
                TextInput,
                TouchableOpacity
            } from 'react-native';
    
        // 例項化入口
        var Login = React.createClass({
            render() {
                return (
                    <View style={styles.container}>
    
                    </View>
                );
            }
        });
    
        // 樣式
        var styles = StyleSheet.create({
            container: {
                flex:1,
                // 背景色
                backgroundColor:'white'
            },
        });
    
        // 註冊輸出元件
        module.exports = Login;
    
    • 使用元件


        import React, { Component } from 'react';
        import {
            AppRegistry,
            StyleSheet
        } from 'react-native';
    
        // 引用Login元件
        var Login = require('./Login');
    
        export default class WeiXin extends Component {
            render() {
                return (
                    <View style={styles.container}>
                        {/* 登入模組 */}
                        <Login></Login>
                    </View>
                );
            }
        }
    

    效果:

    初始化並使用元件.png

  • 我們把介面整體分為上下2大部分,先將這兩部分搞出來

    • 檢視部分


        // 檢視
        var Login = React.createClass({
            render() {
                return(
                    <View style={styles.container}>
                        {/* 上部登入框 */}
                        <View style={styles.loginViewStyle}>
    
                        </View>
                        {/* 下部更多登入方式 */}
                        <View style={styles.moreLoginViewStyle}>
    
                        </View>
                    </View>
                );
            }
        });
    
    • 樣式部分


        // 樣式
        var styles = StyleSheet.create({
    
            container: {
                flex:1,
                // 背景色
                backgroundColor:'white',
                // 對齊方式
                justifyContent:'space-between',
                alignItems:'center'
            },
    
            loginViewStyle: {
                // 尺寸
                width:width * 0.9,
                height:300, // 預設,等會會清除
                // 背景色
                backgroundColor:'red',
                // 上邊距
                marginTop:85
            },
    
            moreLoginViewStyle: {
                // 尺寸
                width:width * 0.9,
                height:40,  // 預設,等會會清除
                // 背景色
                backgroundColor:'red',
                // 下邊距
                marginBottom:30
            }
        });
    

    效果:
    大體結構搭建效果.png

  • 接著我們來具體完成上面登入框中的各個模組

    • 檢視部分


        // 檢視
        var Login = React.createClass({
            render() {
                return(
                    <View style={styles.container}>
                        {/* 上部登入框 */}
                        <View style={styles.loginViewStyle}>
                            {/* 頭像 */}
                            <Image source={{uri:'icon'}} style={styles.iconStyle}></Image>
                            {/* 賬號 */}
                            <Text style={{fontSize:17, margin:10}}>12345</Text>
                            {/* 密碼輸入框 */}
                            <View style={styles.passwordViewStyle}>
                                {/* 左邊圖片 */}
                                <Image source={{uri:'password'}} style={styles.passwordImageStyle}></Image>
                                {/* 右邊輸入框 */}
                                <TextInput style={styles.passwordInputStyle}
                                   placeholder='請填寫密碼'
                        ></TextInput>
                            </View>
                            {/* 登入按鈕 */}
                            <View style={styles.loginButtonStyle}>
                                <Text style={{fontSize:17, color:'white'}}>登 錄</Text>
                            </View>
                            {/* 忘記密碼選項 */}
                            <Text style={{fontSize:15, color:'blue', marginTop: 15}}>登入遇到問題?</Text>
                        </View>
                        {/* 下部更多登入方式 */}
                        <View style={styles.moreLoginViewStyle}>
    
                        </View>
                    </View>
                );
            }
        });
    
    • 樣式部分


        // 樣式
        var styles = StyleSheet.create({
    
            container: {
                flex:1,
                // 背景色
                backgroundColor:'white',
                // 對齊方式
                justifyContent:'space-between',
                alignItems:'center'
            },
    
            loginViewStyle: {
                // 尺寸
                width:width * 0.9,
                // 背景色
                backgroundColor:'red',
                // 上邊距
                marginTop:85,
                // 對齊方式
                alignItems:'center'
            },
    
            iconStyle: {
                // 尺寸
                width:iconWH,
                height:iconWH
            },
    
            passwordViewStyle: {
                // 尺寸
                width:width * 0.9,
                height:passwordViewH,
                // 背景色
                backgroundColor:'yellow',
                // 上邊距
                marginTop:20,
                // 主軸方向
                flexDirection:'row',
                // 對齊方式
                alignItems:'center',
                // 下邊框
                borderBottomWidth:1,
                borderBottomColor:'green'
            },
    
            passwordImageStyle: {
                // 尺寸
                width:passwordImageWH,
                height:passwordImageWH,
                // 圖片填充方式
                resizeMode:'contain',
                // 左邊距
                marginLeft:passwordMargin
            },
    
            passwordInputStyle: {
                // 尺寸
                width:width * 0.9 - (passwordMargin * 3 + passwordImageWH),
                height:passwordViewH,
                // 背景色
                backgroundColor:'white',
                // 左邊距
                marginLeft:passwordMargin
            },
    
            loginButtonStyle: {
                // 尺寸
                width:width * 0.9,
                height:44,
                // 背景色
                backgroundColor:'green',
                // 上邊距
                marginTop:20,
                // 居中對齊
                justifyContent:'center',
                alignItems:'center'
            },
    
            moreLoginViewStyle: {
                // 尺寸
                width:width * 0.9,
                height:40,
                // 背景色
                backgroundColor:'red',
                // 下邊距
                marginBottom:30
            }
        });
    

    效果:
    上部分結構效果.png

  • 現在我們把測試用的背景色去掉看看效果,是不是很接近微信的介面了
    效果:
    上部分實際效果.png

  • 接下來我們來完成下半部分

    • 檢視部分


        {/* 下部更多登入方式 */}
        <View style={styles.moreLoginViewStyle}>
            <Text style={{color:'blue', fontSize:17}}>更多</Text>
        </View>
    
    • 樣式部分


        moreLoginViewStyle: {
            // 尺寸
            width:width * 0.9,
            height:40,
            // 背景色
            backgroundColor:'red',
            // 下邊距
            marginBottom:30,
            // 對齊方式
            alignItems:'center',
            justifyContent:'center'
        }

    效果:
    下部分效果.png

  • 去掉測試時候的背景色,介面就搭建完畢了,效果如下
    效果:
    介面搭建完畢效果.png

  • 接下來就是讓圖片、按鈕、忘記密碼、更多這幾個標籤擁有接受觸控事件並做出反應的能力,這邊就以更多為例

    • 包裝示例


        <TouchableOpacity
            activeOpacity={0.5}
            onPress={() => {alert('點選了更多')}}
        >
            {/* 下部更多登入方式 */}
            <View style={styles.moreLoginViewStyle}>
                <Text style={{color:'blue', fontSize:17}}>更多</Text>
            </View>
        </TouchableOpacity>
    

    效果:
    封裝更多觸控事件.gif

  • 最後將元件完整的程式碼和效果圖貼出來,供參考

        import React, { Component } from 'react';
        import {
            AppRegistry,
            StyleSheet,
            Text,
            View,
            TextInput,
            Image,
            TouchableOpacity
        } from 'react-native';
    
        var Dimensions = require('Dimensions');
        var {width, height} = Dimensions.get('window');
    
        // 常用引數
        var iconWH = 70;
        var passwordViewH = 30;
        var passwordImageWH = 25;
        var passwordMargin = 5;
    
    
        // 檢視
        var Login = React.createClass({
            render() {
                return(
                    <View style={styles.container}>
                        {/* 上部登入框 */}
                        <View style={styles.loginViewStyle}>
                            <TouchableOpacity
                                activeOpacity={0.5}
                                onPress={() => {alert('點選了頭像')}}
                            >
                            {/* 頭像 */}
                            <Image source={{uri:'icon'}} style={styles.iconStyle}></Image>
                            </TouchableOpacity>
                            {/* 賬號 */}
                            <Text style={{fontSize:17, margin:10}}>12345</Text>
                            {/* 密碼輸入框 */}
                            <View style={styles.passwordViewStyle}>
                                {/* 左邊圖片 */}
                                <Image source={{uri:'password'}} style={styles.passwordImageStyle}></Image>
                                {/* 右邊輸入框 */}
                                <TextInput style={styles.passwordInputStyle}
                                   placeholder='請填寫密碼'
                                ></TextInput>
                            </View>
                            <TouchableOpacity
                                activeOpacity={0.5}
                                onPress={() => {alert('點選了登入按鈕')}}
                            >
                            {/* 登入按鈕 */}
                            <View style={styles.loginButtonStyle}>
                                <Text style={{fontSize:17, color:'white'}}>登 錄</Text>
                            </View>
                            </TouchableOpacity>
                            <TouchableOpacity
                                activeOpacity={0.5}
                                onPress={() => {alert('點選了忘記密碼選項')}}
                            >
                            {/* 忘記密碼選項 */}
                            <Text style={{fontSize:15, color:'blue', marginTop: 15}}>登入遇到問題?</Text>
                            </TouchableOpacity>
                        </View>
                        <TouchableOpacity
                            activeOpacity={0.5}
                            onPress={() => {alert('點選了更多')}}
                        >
                        {/* 下部更多登入方式 */}
                        <View style={styles.moreLoginViewStyle}>
                            <Text style={{color:'blue', fontSize:17}}>更多</Text>
                        </View>
                        </TouchableOpacity>
                    </View>
                );
            }
        });
    
        // 樣式
        var styles = StyleSheet.create({
    
            container: {
                flex:1,
                // 背景色
                backgroundColor:'white',
                // 對齊方式
                justifyContent:'space-between',
                alignItems:'center'
            },
    
            loginViewStyle: {
                // 尺寸
                width:width * 0.9,
                // 上邊距
                marginTop:85,
                // 對齊方式
                alignItems:'center'
            },
    
            iconStyle: {
                // 尺寸
                width:iconWH,
                height:iconWH
            },
    
            passwordViewStyle: {
                // 尺寸
                width:width * 0.9,
                height:passwordViewH,
                // 上邊距
                marginTop:20,
                // 主軸方向
                flexDirection:'row',
                // 對齊方式
                alignItems:'center',
                // 下邊框
                borderBottomWidth:1,
                borderBottomColor:'green'
            },
    
            passwordImageStyle: {
                // 尺寸
                width:passwordImageWH,
                height:passwordImageWH,
                // 圖片填充方式
                resizeMode:'contain',
                // 左邊距
                marginLeft:passwordMargin
            },
    
            passwordInputStyle: {
                // 尺寸
                width:width * 0.9 - (passwordMargin * 3 + passwordImageWH),
                height:passwordViewH,
                // 左邊距
                marginLeft:passwordMargin
            },
    
            loginButtonStyle: {
                // 尺寸
                width:width * 0.9,
                height:44,
                // 背景色
                backgroundColor:'green',
                // 上邊距
                marginTop:20,
                // 居中對齊
                justifyContent:'center',
                alignItems:'center'
            },
    
            moreLoginViewStyle: {
                // 尺寸
                width:width * 0.9,
                height:40,
                // 下邊距
                marginBottom:30,
                // 對齊方式
                alignItems:'center',
                justifyContent:'center'
            }
        });
    
        module.exports = Login;
    

    效果:
    整體效果.gif