我使用的是带3个按钮的react-native-elements
ButtonGroup。我需要在应用程序启动时禁用所有按钮,当满足条件时,我需要启用特定的按钮。
我已经禁用了所有使用false标志的按钮,但是我不确定如何使用条件语句和状态来启用特定的按钮。
任何帮助都将不胜感激。
<ButtonGroup
onPress={this.updateIndex}
selectedIndex={selectedIndex}
buttons={buttons}
containerStyle={{ height: 100 }}
//disabled={[0, 1, 2]}
disabled={true}
/>
ADD_DETAILS(index) {
if (index === 0) {
console.log("clicked 0");
this.requestDetails();
}
}
发布于 2019-08-26 13:55:59
您可以将禁用的按钮存储在您的状态中,例如:
this.state = {
selectedIndex: 2,
disabled:[], // you store your disabled buttons here
}
<ButtonGroup
onPress={this.updateIndex}
selectedIndex={selectedIndex}
buttons={buttons}
containerStyle={{height: 100}}
disabled={this.state.disabled}
/>
如果你有这样的ButtonGroup,你可以禁用按钮(例如第一次和第三次点击按钮),如下所示:
<Button
title={"disable first and third buttons"}
onPress={()=>{
this.setState({disabled:[0,2]}); // here you update which buttons you want to disable
}}/>
发布于 2019-08-26 13:28:15
正如文档所说,禁用:
Controls if buttons are disabled. Setting true makes all of them disabled, while using an array only makes those indices disabled.
因此,创建一个结构,如:
disabled={[1,2]}
将仅启用第一个按钮
要更新它,您应该使用状态变量并根据需要对其进行更新,例如:
this.state={
disabled=[0]
}
则禁用的道具将如下所示:
disabled={this.state.disabled}
在你的onPress函数中,你应该按你需要的方式删除/添加你的按钮:
onPress={this.buttonGroupOnPress}
这将把被点击按钮的索引作为参数发送给函数:
buttonGroupOnPress = (index) =>{
//your logic
this.setState({ disabled: /* the new array of disabled buttons indexes */ })
}
来源:https://react-native-training.github.io/react-native-elements/docs/button_group.html#disabled
https://stackoverflow.com/questions/57658445
复制相似问题