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之后禁用这个按钮。
发布于 2017-04-13 20:58:09
您可以使用变量作为标志,例如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>
)
}发布于 2017-04-13 21:25:18
如果您不介意引入下划线或lodash这样的库,您可以将_handlePrice包装在_.once()中。它消除了在组件实例中使用单独的状态的需要。
constructor(props) {
super(props);
this.state = { price: 50000 };
this._handlePrice = _.once(this._handlePrice.bind(this));
}https://stackoverflow.com/questions/43392100
复制相似问题