代码之家  ›  专栏  ›  技术社区  ›  Martin Chinome

组件微调器不工作

  •  0
  • Martin Chinome  · 技术社区  · 9 年前

    我创建了一个组件调用微调器,以便:

    import React, { Component } from 'react';
    
    import {
        Image,
        StyleSheet,
        View,
        Text,
        KeyboardAvoidingView,
        TouchableHighlight,
        Modal,
        Button,
        ActivityIndicator,
    } from 'react-native';
    
    export default class Spinner extends Component{
    
    constructor(props){
        super(props);
        this.state={
            visible:this.props.visible
        };
        this._show=this._show.bind(this);
        this._hide=this._hide.bind(this);
    }
    
    render(){
        return(
            <Modal
                animationType={'none'}
                transparent={true}
                visible={this.state.visible}
                onRequestClose={this.props.onDismissLoadingCallback}>
                <View style={{flex:1}}/>
                <View style={{
                    height:80,
                    width:80,
                    alignItems:'center',
                    justifyContent:'center',
                    backgroundColor:'#3434347f',
                    borderRadius:10,alignSelf:'center'}}>
                    <ActivityIndicator
                        animating={true}
                        size={"large"}
                        color={'white'}
                    />
                </View>
                <View style={{flex:1}}/>
            </Modal>
        );
    }
    _show() {
    this.setState({visible:true});
    }
    
    _hide(){
    this.setState({visible:false});
    }
    }
    

    在该方法中,创建方法\u show()和\u hide()

    class Login extends Component {
        constructor(props) {
            super(props)
            this.state = { visible: false };
        }
    
    _onLoginPress() {
            this.Spinner._show()
                )
        }  
    
     _redirect() {
           this.Spinner._hide()
        }
            render() {
                    return (
             <Spinner visible= {this.state.visible}/>
             <TouchableHighlight style={styles.button}
                        onPress={this._onLoginPress}
                        activeOpacity={1} >
                 <Text style={styles.textButtonLogin}>Visible</Text>
              </TouchableHighlight>
    
              <TouchableHighlight style={styles.button}
                      onPress={this._redirect}
                      activeOpacity={1} >
                <Text style={styles.textButtonLogin}>Not Visible</Text>
              </TouchableHighlight>
    
    )
    }
    

    当在构造函数中设置为true时,它将显示,但当在构造函数中设置为false时,它不会显示,当按下按钮\u onLoginPress时,它会显示,当按下按钮\u redirect()时,它不会显示。

    1 回复  |  直到 9 年前
        1
  •  1
  •   Val    9 年前

    用于使用 <Spinner /> 正确引用,您必须

    <Spinner ref={(ref) => this.Spinner = ref} visible= {this.state.visible} />
    

    然后你的 this.Spinner._show() this.Spinner._hide() 应该有效。

    推荐文章