简体   繁体   English

使用webpack导入css做出反应

[英]import css using webpack in react

I was from angularjs, now picking up react. 我来自angularjs,现在正在接受反应。 Even I was using angular 1.x which is already component based, but it still has template. 即使我使用的是角度1.x,它已经是基于组件的,但它仍然有模板。 But in react the file structure and the way we use to code front end has changed, like instead of spiting files by pages, u make files by component now. 但是在反应中,文件结构和我们用来编写前端的方式已经发生了变化,比如不是按页面吐出文件,而是现在按组件制作文件。 It promotes reusability but does that means how we apply the css also changed? 它提升了可重用性,但这是否意味着我们如何应用css也改变了?

I saw this import { navBar } from 'styles/navbar.css' in navBar.jsx. 我在import { navBar } from 'styles/navbar.css'中看到了这个import { navBar } from 'styles/navbar.css' Hmm how does css work together with JSX? 嗯css如何与JSX一起工作? doest navBar css load that file? doest navBar css加载那个文件? What webpack plugin is needed for that? 那需要什么webpack插件? does it come from default? 它是默认的吗? I'm using react-create-app by facebook so I didn't know much about config. 我正在使用facebook的react-create-app所以我对配置知之甚少。

You use css-loader and style-loader to include CSS files in your webpack bundle. 您可以使用css-loaderstyle-loader在Webpack包中包含CSS文件。 By default it generates some JavaScript code that creates a style element with the contents of the imported CSS file and appends it to the head element in your index.html . 默认情况下,它会生成一些JavaScript代码,用于创建带有导入的CSS文件内容的style元素,并将其附加到index.htmlhead元素。

So you can definitely use external CSS files to style your React components, just make sure that every CSS class is properly namespaced to avoid naming conflicts with the classes of other components. 因此,您绝对可以使用外部CSS文件来设置React组件的样式,只需确保每个CSS类都正确命名空间,以避免命名与其他组件的类冲突。

For example you could adopt the BEM naming scheme. 例如,您可以采用BEM命名方案。 If your component is called NavBar , then the root element of that component might have a className of x-nav-bar (the x prefix is there to avoid clashing with frameworks like bootstrap), and all child elements, if they need to be styled, will then have class names like x-nav-bar__${childName} . 如果您的组件名为NavBar ,那么该组件的根元素可能具有x-nav-barclassNamex前缀用于避免与bootstrap等框架冲突),以及所有子元素(如果需要进行样式设置) ,然后会有像x-nav-bar__${childName}这样的类名。

This kind of import { navBar } from 'styles/navbar.css' is not relevant to JSX but to css-loader . import { navBar } from 'styles/navbar.css'这种import { navBar } from 'styles/navbar.css' JSX无关,而与css-loader无关。 This is a webpack loader that handles css, and it supports cssModules , that allows you to encapsulate selector names in order to avoid css leaks. 这是一个处理css的webpack加载器,它支持cssModules ,允许你封装选择器名称以避免css泄漏。

So, shortly, that import exposes an object with mapping between your selector to unique string (usually an a hash). 因此,很快,该导入会将您的选择器与唯一字符串(通常是哈希)之间的映射公开。

For example: 例如:

// styles.css
.foo {
   color: red;
}

.bar {
   color: green;
}


// Component.jsx
import styles from './styles.css';

console.log(styles);
/* This will print something like
{
  foo: '_h234jh',
  bar: '_234m23'
} 
*/

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

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