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


当前回答

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

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

其他回答

TouchableOpacity扩展了TouchableWithoutFeedback,所以你可以只使用disabled属性:

<TouchableOpacity disabled={true}>
  <Text>I'm disabled</Text>
</TouchableOpacity>

React Native TouchableWithoutFeedback #禁用文档

新的可压API也有一个禁用选项:

<Pressable disabled={true}>
  {({ pressed }) => (
    <Text>I'm disabled</Text>
  )}
</Pressable>

你可以启用和禁用按钮或使用条件或直接在默认情况下,它将是disable: true

 // in calling function of button 
    handledisableenable()
        {
         // set the state for disabling or enabling the button
           if(ifSomeConditionReturnsTrue)
            {
                this.setState({ Isbuttonenable : true })
            }
          else
          {
             this.setState({ Isbuttonenable : false})
          }
        }

<TouchableOpacity onPress ={this.handledisableenable} disabled= 
     {this.state.Isbuttonenable}>

    <Text> Button </Text>
</TouchableOpacity>

TouchableOpacity接收activeOpacity。你可以这样做

<TouchableOpacity activeOpacity={enabled ? 0.5 : 1}>
</TouchableOpacity>

如果它被启用,它看起来会很正常,否则,它看起来就像touchable没有反馈。

这里有一个最简单的解决方案:

你可以在TouchableOpcaity中添加onPressOut事件 做你想做的事。 它不会让用户再次按,直到onPressOut完成

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

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