0

What is the best way to serve css when rendering React components from server side? 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. What is the best way to do this?

2 Answers 2

2

This is not related to server-side at all, but you can use plain javascript object (with camelCase properties) to apply the style:

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks!, just went with defining a separate css file
0

Have you considered using webpack to bundle your files? then you could simply require the css file on your js file, and have it bundled together or separately using plugins

2 Comments

is there an alternative to webpack, for the moment?
Use a <link> to reference to an external css file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.