简体   繁体   English

为什么在 React.js 中为组件使用单独的 CSS 文件

[英]Why to use separate CSS files for components in React.js

I am new to Web Development and currently learning React.js.我是 Web 开发的新手,目前正在学习 React.js。 I learned about how to create Components in React and giving styles to them.我学习了如何在 React 中创建组件并为它们提供样式。

Is it like that if I create separate CSS files for React Components, the app will load faster because the browser has to load small sized CSS files instead of single big file?是不是如果我为 React 组件创建单独的 CSS 文件,应用程序会加载得更快,因为浏览器必须加载小尺寸的 CSS 文件而不是单个大文件?

I want to ask about how React loads the CSS files in the browser and whether I should use separate CSS files for React Components.我想问一下 React 如何在浏览器中加载 CSS 文件,以及我是否应该为 React 组件使用单独的 CSS 文件。

Yes, you want separate files.是的,您需要单独的文件。 No, the reason why you do it isn't for performance (although it certainly can have an impact on performance).不,你为什么这样做的原因是不是表现(虽然它肯定会对性能产生影响)。

You're missing that components should be re-usable .你错过了组件应该是可重用的

If I create an AwesomeWidget component and I want to drop it straight into another project I'm working on then I should be able to do that with as little friction as possible.如果我创建了一个 AwesomeWidget 组件并且我想将它直接放入我正在处理的另一个项目中,那么我应该能够以尽可能少的摩擦来做到这一点。 And you can't do that if its styles are mixed in with application-specific stuff.如果它的样式与特定于应用程序的东西混合在一起,你就不能这样做。 Your components should be, insofar as is possible, independent of what's around them.你的组件应该尽可能独立于它们周围的东西。

Keep the component-specific files separate and let Webpack do the work of wiring it all up for you.将特定于组件的文件分开,让 Webpack 为您完成所有连接工作。 If you find yourself reusing a component enough then move it into its own repo (don't forget you can npm install from a git url if you don't want to publish it to the public package registry).如果你发现自己重复使用了一个组件,然后将它移动到它自己的仓库中(不要忘记,如果你不想将它发布到公共包注册表,你可以从 git url 进行 npm install)。

yes, it is preferred to make every component with its stylesheet and also use CSS modules file to make it scoped.是的,最好使用其样式表制作每个组件,并使用 CSS 模块文件使其具有范围。 also separating your CSS file is for readability only and for you as a developer it has nothing to do with loading faster because your bundler will compress all CSS files into a single one to make it loaded faster.分离你的 CSS 文件只是为了可读性,对于作为开发人员的你来说,这与加载速度无关,因为你的捆绑器会将所有 CSS 文件压缩成一个文件,以使其加载速度更快。 I hope My answer is clear and enough for your question.我希望我的回答对你的问题足够清楚。

In your project folder you will see index.css or app.css, write your style there .在您的项目文件夹中,您将看到 index.css 或 app.css,在那里写下您的样式。 Example例子

.divcontainer{ } .div 容器{}

So in your class now whenever you want to use the css style Use class=“divcontainer” in the tag not className=“divcontainer ”因此,现在在您的班级中,无论何时您想使用 css 样式,请在标签中使用 class="divcontainer" 而不是 className="divcontainer"

You can import the CSS directly into the component.您可以将 CSS 直接导入到组件中。

Example: import './style.css'示例:导入'./style.css'

Or you can use the styled-component library to work with CSS I recommend it.或者你可以使用styled-component库来处理我推荐的 CSS。

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

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