我正在使用react native制作一个android应用程序,我已经使用TouchableOpacity组件来创建按钮。 我使用一个文本输入组件来接受来自用户的文本,并且该按钮只应该在文本输入匹配某个字符串时启用。 我可以想到一种方法来做到这一点,即最初呈现按钮而不使用TouchableOpactiy包装器,并在输入字符串匹配后使用包装器重新呈现。 但我猜有更好的办法。有人能帮忙吗?


当前回答

就这么做

<TouchableOpacity activeOpacity={disabled ? 1 : 0.7} onPress={!disabled && onPress}>
  <View>
    <Text>{text}</Text>
  </View>
</TouchableOpacity>

其他回答

要禁用文本,你必须在文本样式中设置不透明度:0,如下所示:

<TouchableOpacity style={{opacity:0}}>
  <Text>I'm disabled</Text>
</TouchableOpacity>

这个土生土长的基础有一个解决方案:

<Button
    block
    disabled={!learnedWordsByUser.length}
    style={{ marginTop: 10 }}
    onPress={learnedWordsByUser.length && () => {
      onFlipCardsGenerateNewWords(learnedWordsByUser)
      onFlipCardsBtnPress()
    }}
>
    <Text>Let's Review</Text>
</Button>

就这么做

<TouchableOpacity activeOpacity={disabled ? 1 : 0.7} onPress={!disabled && onPress}>
  <View>
    <Text>{text}</Text>
  </View>
</TouchableOpacity>

这似乎是一种可以用高阶分量来解决的问题。我可能是错的,因为我自己也在努力100%地理解它,但也许它会对你有帮助(这里有几个链接)……

http://www.bennadel.com/blog/2888-experimenting-with-higher-order-components-in-reactjs.htm http://jamesknelson.com/structuring-react-applications-higher-order-components/

当输入与字符串不匹配时,您可以在TouchableOpacity中使用禁用道具

<TouchableOpacity disabled = { stringMatched ? false : true }>
    <Text>Some Text</Text>
</TouchableOpacity>

其中stringMatched是一个状态。