简体   繁体   English

在样式化组件中使用 `css` 函数是否会导致性能下降?

[英]Does using the `css` function inside a styled component cause a performance hit?

To get proper code completion, linting, and coloring of styled-components code using Visual Studio Code we've been using the css function as a template wrapper.为了使用 Visual Studio Code 对样式化组件代码进行正确的代码完成、linting 和着色,我们一直使用css函数作为模板包装器。

Here's an example.这是一个例子。 Notice the blue css functions.注意蓝色的css函数。

import styled, { css } from 'styled-components/native'; . .

在此处输入图片说明

Without the css function all the css code is simply green text without any editor smarts.如果没有css函数,所有的 css 代码都是简单的绿色文本,没有任何编辑器智能。

Does this have a performance impact?这对性能有影响吗? Are we be better off foregoing the code editor conveniences and using simple template literals?我们最好放弃代码编辑器的便利性并使用简单的模板文字吗?

Style definitions from styled-components are parsed and extracted as plain CSS at build time into tags in the head of your index.html file. styled-components样式定义在构建时被解析为纯 CSS 并提取到 index.html 文件头部的标签中。

As a result, your html file may get larger, but for there to be a noticeable performance hit, you'd need an enormous amount of styles.因此,您的 html 文件可能会变大,但要显着降低性能,您需要大量的样式。 At that point, you might just consider refactoring.那时,您可能只考虑重构。

My two cents, if its working for you and the tool you chose doesn't introduce new bugs and/or performance issues that threaten your app's vitality, just stick with it until you see a need to change it.我的两分钱,如果它对您有用并且您选择的工具不会引入威胁您应用程序活力的新错误和/或性能问题,请坚持使用它,直到您发现需要更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM