【问题标题】:Invariant Violation: Invariant Violation: React.Children.only expected to receive a single React element childInvariant Violation: Invariant Violation: React.Children.only 期望接收单个 React 元素子元素
【发布时间】:2019-09-07 17:26:16
【问题描述】:

当我使用TouchableOpacity 时,我的代码可以正常工作,但是当我使用TouchableWithoutFeedback 时,我的代码会引发错误。由于我不希望点击时出现这种模糊效果,因此我想改用 TouchableWithoutFeedback

return (
    <View  style={{ ...props.style}}>
        <TouchableWithoutFeedback   style={{...styles.row  }} onPress={toggleExpand}>
             <Text style={{ fontFamily : 'wiproakkurat-bold' , fontSize : RFValue(14) , color : '#434343' , paddingLeft : RFValue(18), ...props.styleText ,}}>{props.title}</Text>
             <Icon style={{paddingRight : RFValue(18)}} name={toggle.expanded ? 'keyboard-arrow-up' : 'keyboard-arrow-down'} size={RFValue(30)} color={'pink'} />
         </TouchableWithoutFeedback>

         <View style={styles.parentHr}/>
         {
             toggle.expanded &&
             <View style={styles.child}>
                 {props.data}  
             </View>
         }
    </View>
)

【问题讨论】:

    标签: react-native touchablewithoutfeedback


    【解决方案1】:

    TouchableWithoutFeedback 上的 documentation 说:

    TouchableWithoutFeedback 仅支持一个孩子。如果您希望拥有多个子组件,请将它们包装在一个视图中。

    确实,TouchableOpacity 确实支持多个孩子(因此您的代码在使用该组件时可以工作),TouchableWithoutFeedback 不支持。但是,您给 TouchableWithoutFeedback 提供了多个子组件(TextIcon),这是无效的。 解决方案应该是简单地将 Text 和 Icon 包装在 View 组件中,或者,如果您不想使用 View,则使用 React.Fragment

    <TouchableWithoutFeedback   style={{...styles.row  }} onPress={toggleExpand}>
        <React.Fragment>
            <Text style={{ fontFamily : 'wiproakkurat-bold' , fontSize : RFValue(14) , color : '#434343' , paddingLeft : RFValue(18), ...props.styleText ,}}>{props.title}</Text>
            <Icon style={{paddingRight : RFValue(18)}} name={toggle.expanded ? 'keyboard-arrow-up' : 'keyboard-arrow-down'} size={RFValue(30)} color={'pink'} />
        </React.Fragment>
    </TouchableWithoutFeedback>
    

    【讨论】:

    • 你的回答给了我解释......但我使用了另一个解决我的问题的黑客,所以我必须标记那个答案被接受,但你的解决方案也是正确的:-)
    【解决方案2】:

    如果您不想在点击/触摸时显示模糊效果。您可以简单地将 activeOpacity={1} 设置为 &lt;TouchableOpacity&gt; 标签

    <TouchableOpacity activeOpacity={1}>
    <Text>Hello</Text>
    </TouchableOpacity>
    

    【讨论】:

      【解决方案3】:

      您不能在 Touchables 中使用两个或更多元素。 要解决您的问题,您必须使用 &lt;View&gt;&lt;/View&gt;&lt;React.Fragment&gt;&lt;/React.Fragment&gt; 包装您的元素 像这样:

         <TouchableWithoutFeedback   style={{...styles.row  }} onPress={toggleExpand}>
              <View>
                    <Text style={{ fontFamily : 'wiproakkurat-bold' , fontSize : RFValue(14) , color : '#434343' , paddingLeft : RFValue(18), ...props.styleText ,}}>{props.title}</Text>
                    <Icon style={{paddingRight : RFValue(18)}} name={toggle.expanded ? 'keyboard-arrow-up' : 'keyboard-arrow-down'} size={RFValue(30)} color={'pink'} />
              </View>
         </TouchableWithoutFeedback>
      

      或者

      
         <TouchableWithoutFeedback   style={{...styles.row  }} onPress={toggleExpand}>
              <React.Fragment>
                    <Text style={{ fontFamily : 'wiproakkurat-bold' , fontSize : RFValue(14) , color : '#434343' , paddingLeft : RFValue(18), ...props.styleText ,}}>{props.title}</Text>
                    <Icon style={{paddingRight : RFValue(18)}} name={toggle.expanded ? 'keyboard-arrow-up' : 'keyboard-arrow-down'} size={RFValue(30)} color={'pink'} />
              </React.Fragment>
         </TouchableWithoutFeedback>
      
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-02-23
        • 2017-09-16
        • 2018-05-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多