简体   繁体   English

如何在React Server Side Rendererd App上使用样式表?

[英]How to use Stylesheets on React Server Side Rendererd App?

I am trying to convert my ReactJS application to Universal App with server-side rendering, but there is something that I don't understand. 我正在尝试通过服务器端渲染将ReactJS应用程序转换为通用应用程序,但是有些我不理解。 What is the best way to use and import styles to my components? 使用样式并将样式导入到组件的最佳方法是什么? Can I just import external stylesheet in HTML and use this all over my components? 我可以仅以HTML格式导入外部样式表并在所有组件中使用它吗? or import specific stylesheet to every component and use it as separate variables like this : 或将特定的样式表导入每个组件并将其用作单独的变量,如下所示:

import s from './about.css';

export default class About extends Component {
  render() {
    return (
      <div>
        <div className={`${s.divone} row`}>
          <span>Hello world</span>
        </div>
        <div className={s.divtwo}>
          <span>bye world</span>
        </div>
      </div>
    );
  }
}

with the second approach, I have noticed that every time I do refresh my page, the styles are kinda too slow to load, and for one second I see my page without any CSS, and then I see my full page with CSS. 使用第二种方法时,我已经注意到,每次刷新页面时,样式都加载得太慢了,一秒钟后,我看到页面没有任何CSS,然后看到了整个CSS页面。 There is any way to bypass that without external and global stylesheet? 没有外部和全局样式表,有什么方法可以绕过它? Like for example, Airbnb does. 例如,Airbnb确实如此。 Maybe with some way to wait until all styles are loaded? 也许以某种方式等待所有样式加载完毕?

Also after I moved my app to server-side render, I get some strange warnings like this : 同样在将我的应用程序移至服务器端渲染后,我得到了一些奇怪的警告,如下所示:

在此处输入图片说明

You can use classnames , a simple and small JS library to import and use css in your react components: 您可以使用classnames (一个简单的小型JS库)在react组件中导入和使用css:

import styles from './css/yourcss.css'
import classnames from 'classnames/bind'

const cx = classnames.bind(styles)

// use in react component
<div className={cx('mydiv')}></div>

cx can have any JS expression inside it or just plain class name from css you have imported styles from. cx可以在其中包含任何JS表达式,也可以只是您从中导入样式的css中的纯类名。

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

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