【问题标题】:Is there any performance drawback in using functions inside StyleSheet?在 StyleSheet 中使用函数是否有任何性能缺陷?
【发布时间】:2019-06-21 10:27:25
【问题描述】:

为了简化代码并将道具传递给我的样式,我想出了这个解决方案

const styles = StyleSheet.create({
  someNormalStyle: {
    backgroundColor: 'red',
  },
  dynamicStyle(color) {
   return {
    backgroundColor: color
   }
  }
})

然后我会在我的组件中正常使用它,即

<View style={style.someNormalStyle} />
<View style={style.dynamicStyle('blue')} />

这似乎可行,我只是想验证这样做是否会影响性能?

【问题讨论】:

    标签: javascript reactjs react-native styles


    【解决方案1】:

    你真的race the horses了吗?

    如果你这样做了,你可能会注意到一个函数调用不会花费你那么多,引擎甚至可能内联它。

    “简化代码”...是的,这就是重点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 2020-02-14
      • 2012-01-06
      • 2021-12-19
      相关资源
      最近更新 更多