简体   繁体   English

sass 与创建反应应用程序

[英]sass with create react app

Can someone please tell me if it is worth using Sass/SCSS with React?有人可以告诉我在 React 中使用 Sass/SCSS 是否值得?

I finally set up SCSS to work with Create-React-App without ejecting, but it doesn't seem to work well since it is recommended to use CSS modules with each component.我最终将 SCSS 设置为与 Create-React-App 一起使用而不弹出,但它似乎效果不佳,因为建议对每个组件使用 CSS 模块。

How would I go about sharing variables, mixins, etc. Is it better just to use CSS?我将如何共享变量、mixin 等。仅使用 CSS 会更好吗?

Any help would be greatly appreciated.任何帮助将不胜感激。

The 2. version of create-react-app supports Sass now. 2. 版本的 create-react-app 现在支持 Sass。 Install node-sass (eg npm install node-sass ) to enable it.安装 node-sass(例如npm install node-sass )以启用它。 For further instructions, check out the documentation .如需进一步说明,请查看文档 If you need an example, check out this application's Navigation component .如果您需要示例,请查看此应用程序的导航组件

I highly recommend using scss, as you can still use CSS-Modules with SCSS.我强烈推荐使用 scss,因为您仍然可以将 CSS-Modules 与 SCSS 一起使用。 I'm not familiar with the create react app setup, but usually you'd configure webpack with style-loaders like so:我不熟悉 create react 应用程序设置,但通常您会使用样式加载器配置 webpack,如下所示:

  {
    test: /\.scss$/,
    loaders: [
      'style-loader',
      { loader: 'css-loader', options: { modules: true } },
      'sass-loader',
    ],
    exclude: [srcDir + '/assets/scss/']
  },

The loaders will execute backwards, starting with the sass-loader which will transpile your scss to normal css, then with the css-loader and the option "modules: true" these styles will be made available to use as modules.加载器将向后执行,从 sass-loader 开始,它将您的 scss 转换为普通 css,然后使用 css-loader 和选项“modules: true”,这些样式将可用作模块。

Of course it could look a bit different but that would be the basic approach to use css modules with scss.当然,它可能看起来有点不同,但这将是将 css 模块与 scss 一起使用的基本方法。

To share variables and mixins I always use a global scss file, which will be required in the app (eg in the app.js).为了共享变量和混合,我总是使用一个全局 scss 文件,这在应用程序中是必需的(例如在 app.js 中)。 However you'll still have to import that file/s in every component-scss where you need the variables and mixins.但是,您仍然必须在需要变量和混合的每个组件 scss 中导入该文件。

  // GLOBAL STYLES
  {
    test: /\.scss$/,
    loaders: [
      'style-loader',
      'css-loader',
      'sass-loader',
    ],
    include: [srcDir + '/assets/scss/']
  },

Alternatively you could use PostCSS-Properties to make your components even more flexible (eg to provide different themes for your app, without explicitly importing the files in your component styles) but afaik those are not supported by IE and would require you to add specific postcss loaders.或者,您可以使用 PostCSS-Properties 使您的组件更加灵活(例如,为您的应用程序提供不同的主题,而无需显式导入组件样式中的文件),但 IE 不支持这些文件,需要您添加特定的 postcss装载机。

see http://cssnext.io/features/#custom-properties-varhttp://cssnext.io/features/#custom-properties-var

The flexibility and maintainability that SASS provides is very useful for big projects especially with react. SASS 提供的灵活性和可维护性对于大型项目非常有用,尤其是 React。

However, don't let yourself be influenced by my or the opinion of others - you're free to use the things you're most comfortable with and should always question the way things are done.但是,不要让自己受到我或其他人的意见的影响 - 您可以自由使用您最喜欢的东西,并且应该始终质疑做事的方式。

create-react-app V2 对应部分文档:(支持开箱即用) https://facebook.github.io/create-react-app/docs/adding-a-sass-stylesheet#docsNav

Using Sass/SCSS is pretty much completely unlinked to React.使用 Sass/SCSS 几乎与 React 完全无关。 Sass is compiled to regular CSS, and then you use it as such within your HTML or React. Sass 被编译为常规 CSS,然后您可以在 HTML 或 React 中使用它。

As for sharing variables, when you use @import it basically just takes everything from the file your importing and pastes it in, so you could just make a _variables.sccs file and import it into each file where you want to use your global variables.至于共享变量,当您使用@import它基本上只是从您导入的文件中获取所有内容并将其粘贴到其中,因此您只需创建一个_variables.sccs文件并将其导入到您想要使用全局变量的每个文件中。

//variables.scss
$primary: red;
$secondary: blue;


//main.scss
@import 'variables';

body {
    background-color: $primary;
    color: $secondary;
}

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

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