StyleSheet.create 的重点是什么 [英] What is the point of StyleSheet.create

查看:70
本文介绍了StyleSheet.create 的重点是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 React Native 文档/教程,我想知道 StyleSheet.create 函数的意义是什么.

例如教程有如下代码:

const 样式 = StyleSheet.create({大蓝色: {颜色:'蓝色',fontWeight: '粗体',字体大小:30,},红色的: {红色',},});

但我不明白这和:

const 样式 = {大蓝色: {颜色:'蓝色',fontWeight: '粗体',字体大小:30,},红色的: {红色',},};

解决方案

TL;DR 尽可能使用 StyleSheet.create().

Nico 的回答是正确的,但还有更多.

总结:

  1. 它验证 Nico 提到的样式
  2. 文档中所述:

<块引用>

从样式对象制作样式表可以通过 ID 引用它,而不是每次都创建一个新的样式对象.

  1. 也在文档中提到:

<块引用>

它还允许仅通过桥发送一次样式.所有后续使用都将引用一个 id(尚未实现).

您可能知道,通过网桥发送数据是一项成本非常高的操作,会对应用程序的性能产生重大影响.因此,使用 StyleSheet.create() 可以减少桥梁上的压力.

I'm reading the React Native docs / tutorial, and I'm wondering what the point of the StyleSheet.create function is.

For example, the tutorial has the following code:

const styles = StyleSheet.create({
  bigblue: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
  },
  red: {
    color: 'red',
  },
});

But I don't understand the difference between that and:

const styles = {
  bigblue: {
    color: 'blue',
    fontWeight: 'bold',
    fontSize: 30,
  },
  red: {
    color: 'red',
  },
};

解决方案

TL;DR Always use StyleSheet.create() when you can.

The answer by Nico is correct, but there is more to it.

To summarize:

  1. It validates the styles as mentioned by Nico
  2. As mentioned in the documentation:

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.

  1. Also mentioned in the documentation:

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

As you might know, sending the data across the bridge is a very costly operation that has significant impact on the performance of the application. So, using StyleSheet.create() you reduce the strain on the bridge.

这篇关于StyleSheet.create 的重点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆