当前位置 : 主页 > 网页制作 > React >

reactjs – 在react-native中oneClick后禁用TouchableOpacity按钮?

来源:互联网 收集:自由互联 发布时间:2021-06-15
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 onP
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>
        )
 }}

所以,我想要的是,一旦价格下降,我想在一次点击后禁用我的按钮,这样用户就不能一次又一次地降低价格.我想在oneCLick之后禁用该按钮.

你可以使用变量作为标志,例如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>
        )
    }
}

按这种方式按钮只工作一次.
您可以按下后删除TouchableOpacity:

render() { 
    if (!this.pressed)
        return(
            <TouchableOpacity onPress={this._handlePrice} >
                <Text> Offer for you </Text>
            </TouchableOpacity>
        )
    else
        return(
            <View>
                <Text> Offer for you </Text>
            </View>
        )
}
网友评论