【问题标题】:How to apply style as default to my component如何将样式默认应用到我的组件
【发布时间】:2019-11-05 15:10:05
【问题描述】:

我在 React Native 中有一个嵌套在 a 中。有没有办法告诉 React Native 为特定(或所有)视图元素内的所有嵌套文本元素设置样式?

<View style={[container.header,container.super]}><Text>overview</Text></View>

我不想声明 style={text.headerText} 或类似于视图元素内的每个文本元素。也许有类似的东西

.container.header > 文本 { ... }

就像在经典的 .css 中一样

【问题讨论】:

标签: javascript react-native


【解决方案1】:

一个选项可以是创建一个组件

function MyTextComponent(props) {
  return (<Text style={style.myTxtStyle}>{props.children}</Text>);
}

然后像这样使用 MyTextComponent 而不是使用文本组件

<MyTextComponent>Some text</MyTextComponent>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-26
    • 2012-05-09
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多