简体   繁体   English

React服务器端渲染和CSS

[英]React Server Side Rendering and CSS

What is the best way to serve css when rendering React components from server side? 从服务器端渲染React组件时,服务CSS的最佳方法是什么? Currently i have a style tag defined in the head of the parent component. 目前,我在父组件的头部定义了一个样式标签。 It looks like the following 看起来如下

            <style>{"\
            .test-cases{\
            border:2px solid black;\
            margin-left:30px\
            background-color:blue;\
            \
            }\
                .describe{\
                  margin-left:90px;\
                  background-color:yellow;\
                }\
              "}</style>

As you can see this can get nasty, as i start applying more css properties. 如您所见,当我开始应用更多的CSS属性时,这可能会令人讨厌。 What is the best way to do this? 做这个的最好方式是什么?

This is not related to server-side at all, but you can use plain javascript object (with camelCase properties) to apply the style: 这一点与服务器端完全无关,但是您可以使用普通的javascript对象(带有camelCase属性)来应用样式:

style = {marginLeft: '30px',  backgroundColor: 'blue'}
...

<div style={style}>
...
</div>

Then you define your style in a separate file and require it into the main view. 然后,您在一个单独的文件中定义样式,并将其要求进入主视图。 Without needing webpack. 不需要webpack。

Have you considered using webpack to bundle your files? 您是否考虑过使用webpack捆绑文件? then you could simply require the css file on your js file, and have it bundled together or separately using plugins 那么您只需要在js文件中使用css文件,然后将其捆绑在一起或使用插件分别打包即可

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

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