【问题标题】:How to combine a styles object with an inline style in React Native?如何在 React Native 中将样式对象与内联样式结合起来?
【发布时间】:2018-07-27 10:29:36
【问题描述】:

我正在尝试在不修改样式对象的情况下将内联样式 transform: [{ rotate: '180deg'}] 与现有样式对象 styles.buttonText 结合起来。我尝试了以下方法:

<Text style={{...styles.buttonText, transform: [{ rotate: '180deg'}]}}>^</Text>

<Text style={{...styles.buttonText, ...{transform: [{ rotate: '180deg'}]}}}>^</Text>

<Text style={Object.assign({}, styles.buttonText, {transform: [{ rotate: '180deg'}]})}>^</Text>

但我不断收到同样的错误消息:

TypeError:在这种环境下,assign 的来源必须是 目的。此错误是性能优化而不是规范 合规。

有谁知道发生了什么或者我怎样才能让它发挥作用?

【问题讨论】:

    标签: javascript css reactjs react-native


    【解决方案1】:

    如果你想在一个元素上组合样式,你必须将一个数组传递给 style 属性。数组中的最后一项将优先。

    例如:

    <Text style={[styles.buttonText, {transform: [{ rotate: '180deg' }]}]}>^</Text>
    

    见风格docs

    【讨论】:

      【解决方案2】:

      我得到了它的工作:

      <Text style={[styles.buttonText, {transform: [{ rotate: '180deg' }]}]}>^</Text>
      

      【讨论】:

      • 为什么你会接受你自己的答案,而它实际上正是 devlighted 的答案.....
      猜你喜欢
      • 2017-02-28
      • 2022-11-23
      • 2017-07-19
      • 2020-02-15
      • 1970-01-01
      • 2019-01-26
      • 1970-01-01
      • 2017-01-13
      • 2021-10-27
      相关资源
      最近更新 更多