加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 我为什么要使用React原生StyleSheet?

发布时间:2020-12-15 20:33:41 所属栏目:百科 来源:网络整理
导读:我为什么要用这个: const styles = StyleSheet.create({ ...}); 而不是这个: const styles = { ...}; StyleSheet提供的属性对我来说似乎不太需要. 解决方法 主要优势似乎与性能有关: Making a stylesheet from a style object makes it possible to refer
我为什么要用这个:

const styles = StyleSheet.create({
    ...
});

而不是这个:

const styles = {
    ...
};

StyleSheet提供的属性对我来说似乎不太需要.

解决方法

主要优势似乎与性能有关:

Making a stylesheet from a style object makes it possible to refer to
it by ID instead of creating a new style object every time.

It also
allows to send the style only once through the bridge. All subsequent
uses are going to refer an id (not implemented yet).

其他优点可能是:

>支柱absoluteFill用来

create overlays with position absolute and zero positioning


>扁平化功能

Flattens an array of style objects,into one aggregated style object.

来源:https://facebook.github.io/react-native/docs/stylesheet.html

从我所看到的,当设置组件的样式时,即使传递对象数组,它们也都工作(普通对象,StyleSheet对象).

The style prop can be a plain old JavaScript object. That’s the
simplest and what we usually use for example code. You can also pass
an array of styles – the last style in the array has precedence,so
you can use this to inherit styles.

资料来源:http://facebook.github.io/react-native/docs/style.html#content

所以对于简单的样式,我会使用普通对象,对于复杂的样式,我会使用StyleSheet对象.

我在使用StyleSheet对象时发现的唯一缺点是你不能像这样的样式进行比较.myNiceComponent.backgroundColor ===’blue’

关于这个缺点的更多细节:
Why can’t we check a style attribute of a react-native app?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读