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

时间:2018-10-02 07:24:04

标签: javascript reactjs react-native

如果我的数组为空,我想disable TouchableHighlight,如果我的数组有值,我想把它改回enable

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>

以上是我的代码。我认为我做对了,但是没有用。任何建议或意见,将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以做到

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>
    );
}