Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable TouchableOpacity button after oneClick in react-native?

export default decreasePrice extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
    price : 50000
   }
 };
 _handlePrice = () => {
     this.setState({price : this.state.price - 2000});
 }
render() { 
    return( <div>
        <TouchableOpacity onPress={this._handlePrice} >
            <Text> Offer for you </Text>
        </TouchableOpacity>
        )
 }}

So , what I want is, I want to disable my button after oneclick once price is decresed , so that user can not decrese price again and again. I want to disable the button after oneCLick.

like image 698
Abhishek Kumar Pandey Avatar asked Dec 18 '22 08:12

Abhishek Kumar Pandey


1 Answers

you can use a variable as flag, for example this.pressed:

export default decreasePrice extends React.Component {
    constructor(props) {
        super(props);
        this.pressed = false;
        this.state = {
          price : 50000
      }
  };
    _handlePrice = () => {
        if (!this.pressed){
           this.pressed = true;
           this.setState({price : this.state.price - 2000});
        }
    }
    render() { 
        return( 
            <TouchableOpacity onPress={this._handlePrice} >
                <Text> Offer for you </Text>
            </TouchableOpacity>
        )
    }
}

in this way button just working for one time. you alse can remove TouchableOpacity after press:

render() { 
    if (!this.pressed)
        return(
            <TouchableOpacity onPress={this._handlePrice} >
                <Text> Offer for you </Text>
            </TouchableOpacity>
        )
    else
        return(
            <View>
                <Text> Offer for you </Text>
            </View>
        )
}
like image 107
Meysam Izadmehr Avatar answered Dec 31 '22 02:12

Meysam Izadmehr