首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在React Native中查看换行文本标记抱怨文本字符串

问在React Native中查看换行文本标记抱怨文本字符串
EN

Stack Overflow用户
提问于 2019-10-28 11:14:47
回答 1查看 41关注 0票数 0

我在组件中有这段代码,但问题是当我删除外部View时,RN会抱怨Invariant Violation: Text strings must be rendered within a component..。好吧,我将文本包装在一个view和text标记中。为什么会出现错误。下面的代码片段运行良好,当外部视图关闭时会出现错误。

代码语言:javascript
复制
<View>
    {
        badgeCount > 0 && (<View style={{
                position: 'absolute',
                right: -6,
                top: -3,
                backgroundColor: 'red',
                borderRadius: 6,
                width: 12,
                height: 12,
                justifyContent: 'center',
                alignItems: 'center'
            }}>
            <Text style={{
                    color: 'white',
                    fontSize: 10,
                    fontWeight: 'bold'
                }}>
                {badgeCount}
            </Text>
        </View>)
    }
</View>

全渲染方法-

代码语言:javascript
复制
render() {
    const {name, badgeCount, color, size} = this.props;
    return (<View style={{
            width: 24,
            height: 24,
            margin: 5
        }}>
        <Ionicons name={name} size={size} color={color}/> {
            badgeCount > 0 && (<View style={{
                    position: 'absolute',
                    right: -6,
                    top: -3,
                    backgroundColor: 'red',
                    borderRadius: 6,
                    width: 12,
                    height: 12,
                    justifyContent: 'center',
                    alignItems: 'center'
                }}>
                <Text style={{
                        color: 'white',
                        fontSize: 10,
                        fontWeight: 'bold'
                    }}>
                    {badgeCount}
                </Text>
            </View>)
        }

    </View>);
}
EN

回答 1

Stack Overflow用户

发布于 2019-10-28 13:52:34

在render方法中,如果我们必须根据一个条件来渲染一些东西,那么我们使用花括号来添加条件。

代码语言:javascript
复制
render(){
  return (
      <View>
       {toShowText &&
       <Text>Hello viewing</View>
       }
      </View>
   )}

在您的示例中,当您移除外部视图时,curly braces.Render方法将其视为任何文本,并强制您在component.That中换行。这就是为什么要使用条件呈现,您必须在视图容器中用大括号换行

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58585388

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档