RN自定義Button

系統(tǒng)的Button不好看而且不容易擴(kuò)展,所以在這里使用TouchableOpacity自定義一個(gè)Button

import React, {Component} from "react";
import {Text, StyleSheet, TouchableHighlight, TouchableOpacity, View} from "react-native";

export default class Button extends Component {
    state = {
        status: 1,
        disabled:false,
    };
    /*customPressHandler(){
        //不建議這樣定義方法
        alert('你按下了按鈕');
    }*/
    customPressHandler = () => {
        //自定義的方法,請(qǐng)使用屬性來定義,這里自定的把this綁定到這個(gè)方法上
        alert('你按下了按鈕' + this.state.status);
    };
    onPress = ()=>{
        const {onPress } =this.props;
        onPress ? onPress():"";
    };
    enable=()=>{
        this.setState({
          disabled:false,
      })
    };
    disabled =()=>{
        this.setState({
            disabled:true,
        })
    };
    render(){
        const {name, backgroundColor} = this.props;
        return (
            <View style={{flex: 1, justifyContent: 'center', alignItems: 'center',marginBottom:50}}>
                <TouchableOpacity
                    disabled={this.state.disabled}
                    style={[styles.button,
                        {backgroundColor:backgroundColor?backgroundColor:'green'},
                        this.state.disabled && styles.disabled]}
                    onPress={this.onPress}>
                    <Text style={styles.buttonText}>{name ? name:"確定"}</Text>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    button: {
        width: 150,
        height: 40,
        borderRadius: 20,
        backgroundColor: 'green',
        justifyContent: 'center',
        overflow: 'hidden'
    },
    buttonText: {
        textAlign: 'center'
    },
    disabled:{
        backgroundColor:'gray',
    },
});

使用這個(gè)自定義的Button

import React, {Component} from "react";
import {Modal, Text, StyleSheet, TouchableHighlight, TouchableOpacity, View} from "react-native";
import Button from "./component/Button";

export default class ModalExample extends Component {
    state = {
        modalVisible: false,
        status: 1
    };

    setModalVisible(visible) {
        this.setState({modalVisible: visible});
    }
    fetchData=()=>{
        //禁用按鈕
        this.refs.button.disabled();
        this.timer = setTimeout(()=>{
            //獲取完數(shù)據(jù)后啟用按鈕
            this.refs.button.enable();
        },3000);
    };
    componentWillUnmount() {
        // 請(qǐng)注意Un"m"ount的m是小寫
        // 如果存在this.timer,則使用clearTimeout清空。
        // 如果你使用多個(gè)timer,那么用多個(gè)變量,或者用個(gè)數(shù)組來保存引用,然后逐個(gè)clear
        this.timer && clearTimeout(this.timer);
    }
    render() {
        return (
            <View style={{marginTop: 22}}>
                <Button />
                <Button name='取消' backgroundColor='red' onPress={()=>{alert("1")}}/>
                {/*ref就相當(dāng)于html中的id,標(biāo)記和引用組件*/}
                <Button ref='button' onPress={this.fetchData}/>
            </View>
        );
    }
}

這里最后一個(gè)Button使用了ref,它就相當(dāng)于html中的id,標(biāo)記和引用組件,在這里主要是模擬點(diǎn)擊按鈕后網(wǎng)絡(luò)請(qǐng)求數(shù)據(jù)前后的按鈕禁用和啟用

<Button ref='button' onPress={this.fetchData}/>
fetchData=()=>{
    //禁用按鈕
    this.refs.button.disabled();
    this.timer = setTimeout(()=>{
        //獲取完數(shù)據(jù)后啟用按鈕
        this.refs.button.enable();
    },3000);
};

這個(gè)是使用ref來實(shí)現(xiàn)的,還可以使用回調(diào)方式:

在自定義組件Button中的onPress方法中這樣改寫

onPress = ()=>{
    const {onPress } =this.props;
    this.disabled();
    onPress ? onPress(this.enable):"";
};

然后在使用中的fetchData方法中這樣改寫

fetchData=(enabledCallback)=>{
    this.timer = setTimeout(()=>{
        //獲取完數(shù)據(jù)后啟用按鈕
        enabledCallback();
    },3000);
};

上面的onPress(this.enable)方法中傳入的this.enable會(huì)在fetchData中使用enabledCallback接收,然后執(zhí)行

gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 不管是Android還是ios,Button控件都在這兩個(gè)原生開發(fā)中都已經(jīng)被封裝好了,我們可以直接使用。但是在RN...
    BlainPeng閱讀 6,307評(píng)論 2 4
  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請(qǐng)求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,236評(píng)論 3 119
  • 1 IOS組件 1.1 iOS活動(dòng)指示器 1.1.1 Props animatingbool型 顯示指示器(tru...
    Kevin_Junbaozi閱讀 2,117評(píng)論 0 2
  • 這是歡樂嗎? 興奮,失眠, 在輾轉(zhuǎn)的思緒中 不安。想哭 想笑,想喊。 又怕剛一出唇 就變成長了翅膀的風(fēng) 那么大的空...
    羞蓮閱讀 257評(píng)論 0 5
  • 對(duì)生活越來越有追求,你說的,我做的,你做的,我看的,都在腦海里,說一句你真的很厲害! 目標(biāo) 健康生活 從一句早安開...
    換氧閱讀 103評(píng)論 0 0

友情鏈接更多精彩內(nèi)容