React-Native 自定义Button 获取远程数据
src/component/Button.js import React,{Component} from 'react';import { StyleSheet, Text, TouchableOpacity } from 'react-native'; export default class Button extends Component { constructor(props) { super(props); this.state = { disabled: false, }; } onPress = () => { const { onPress } = this.props; //onPress(); //控制按钮的状态方式一 onPress(this.enable); //控制按钮的状态方式二 异步传递一个方法但不立即执行 }; enable = () => { this.setState({ disabled: false, }); }; disable = () => { this.setState({ disabled: true, }); }; render() { const { text } = this.props; return ( <TouchableOpacity disabled={this.state.disabled} style={[styles.button,this.state.disabled && styles.disabled]} onPress={this.onPress}> <Text style = {styles.buttonText}>{text}</Text> </TouchableOpacity> ); } } const styles = StyleSheet.create({ button: { marginTop: 100, height: 40, width: 200, borderRadius: 20, justifyContent: 'center', alignItems: 'center', backgroundColor: 'green', }, buttonText: { fontSize: 16, color: 'white', disabled: { backgroundColor: 'gray', }); App.js import React,{Component} from 'react'; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |