gpt4 book ai didi

javascript - React-native- 如果数组为空,则有条件地禁用 TouchableHighlight

转载 作者:行者123 更新时间:2023-11-29 23:16:29 25 4
gpt4 key购买 nike

如果我的数组为空,我想禁用 TouchableHighlight,如果我的数组有值,我想将其恢复为启用

this.state = {
modalVisible: false,
array:[],
}
}

toggleModal(visible) {
this.setState({modalVisible: visible})
}

<TouchableHighlight
underlayColor="transparent"
onPress = {() => {
if(this.state.array == undefined || this.state.array.length == 0){
this.toggleModal(this.state.modalVisible)}
else {
this.toggleModal(!this.state.modalVisible)}
}}>
<Text>close</Text>
</TouchableHighlight>

上面是我的代码。我认为我做对了,但它不起作用。任何建议或意见将不胜感激。

最佳答案

你可以这样做

this.state =
{
modalVisible: false,
array: []
}

toggleModal = visible => this.setState({modalVisible: visible})

render = () =>
{
return (
<TouchableHighlight
underlayColor="transparent"
disabled={this.state.array.length === 0}
onPress = {() =>
{
if(this.state.array == undefined || this.state.array.length == 0)
this.toggleModal(this.state.modalVisible);
else
this.toggleModal(!this.state.modalVisible);
}}>
<Text>close</Text>
</TouchableHighlight>
);
}

关于javascript - React-native- 如果数组为空,则有条件地禁用 TouchableHighlight,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52603699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com