reactjs - 在 react native 中单击一次后禁用 TouchableOpacity 按钮?

标签 reactjs 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>
        )
 }}

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

最佳答案

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

关于reactjs - 在 react native 中单击一次后禁用 TouchableOpacity 按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43392100/

相关文章:

reactjs - 如何聚焦位于子组件中的输入字段

javascript - react js : import component to route

Javascript React 组件根据条件返回 2 个值

ios - 连接到 Apple Music

react-native - "Module does not exist in the module map or in these directories"在 native react

react-native - React Native Fetch 返回网络请求失败

javascript - Undefined 不是一个对象(评估 'this. state.input' )

node.js - 无法在Azure应用程序服务上部署React JS应用程序

javascript - redux-form 无法读取未定义的属性 'any'

android - React Native Android 表情符号兼容