【问题标题】:React Native - Default style for elementsReact Native - 元素的默认样式
【发布时间】:2015-11-18 05:37:37
【问题描述】:

React Native 中有没有一种方法可以为标准元素设置样式,而无需为每个元素添加样式标签?例如,我想更改这些样式:

<Text>This is test</Text>
<Text>This is also text</Text>
<Text>And so is this</Text>

据我了解,说边距缩进的唯一方法是这样做:

<Text style={styles.indentText}>This is test</Text>
<Text style={styles.indentText}>This is also text</Text>
<Text style={styles.indentText}>And so is this</Text>

在普通的网站 CSS 中,您显然可以为所有 HTML 元素设置一些样式,而无需为其添加类或 ID。在 React Native 中是如何实现的?

【问题讨论】:

    标签: react-native


    【解决方案1】:

    你可以为它设置一个抽象层:

    IndentedText = React.createClass({
      render: () {
        <Text style={styles.indentText}>{this.props.text}</Text>
      }
    })
    

    然后在你的其他组件中:

    <IndentedText text={"This is a text"} />
    <IndentedText text={"This is also a text"} />
    <IndentedText text={"And so is this"} />
    

    【讨论】:

    • 所以真的不可能为通用元素提供默认样式设置吗?感谢您的建议。
    • 我不想修改原来的 Text 组件。只需像我建议的那样使用自定义组件,您就应该设置好。我相信您不会希望所有文本组件都具有完全相同的样式...
    • 好吧,我只是认为纯粹使用 css 样式是可能的。就像在网页设计中一样: div {margin-left: 20px;} 无需更改任何主要显示的元素。我猜 React Native 做不到。
    猜你喜欢
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多