首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在react-native中禁用oneClick之后的TouchableOpacity按钮?

在react-native中禁用oneClick之后的TouchableOpacity按钮?
EN

Stack Overflow用户
提问于 2017-04-13 20:16:09
回答 2查看 9.5K关注 0票数 4
代码语言:javascript
复制
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之后禁用我的按钮,这样用户就不能一次又一次地降低价格。我想在oneCLick之后禁用这个按钮。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-13 20:58:09

您可以使用变量作为标志,例如this.pressed

代码语言:javascript
复制
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:

代码语言:javascript
复制
render() { 
    if (!this.pressed)
        return(
            <TouchableOpacity onPress={this._handlePrice} >
                <Text> Offer for you </Text>
            </TouchableOpacity>
        )
    else
        return(
            <View>
                <Text> Offer for you </Text>
            </View>
        )
}
票数 7
EN

Stack Overflow用户

发布于 2017-04-13 21:25:18

如果您不介意引入下划线或lodash这样的库,您可以将_handlePrice包装在_.once()中。它消除了在组件实例中使用单独的状态的需要。

代码语言:javascript
复制
constructor(props) {
  super(props);
  this.state = { price: 50000 };
  this._handlePrice = _.once(this._handlePrice.bind(this));
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43392100

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档