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/