This is my react render function
render:function(){ return ( <div> <p className="rr">something</p> <style> .rr{ color:red; } </style> </div> ) }
This gives me this error
"JSX: Error: Parse Error: Line 22: Unexpected token :"
What's wrong here? Can I embed full normal css into a react component?
Did you know that you can style a React component with raw CSS and no extra files? There are many ways to style a component in React, but some are more popular than others.
JSX gives us the convenience of combining HTML syntax and JavaScript under one file in React. However, our stylesheets always existed in a separate directory. With inline styles, you have to option to combine CSS syntax with JSX code.
So to convert it to browser understandable JavaScript code, we use a tool like Babel which is a JavaScript compiler/transpiler. You can set up your own babel configuration using Webpack as I show in this article. Or you can use create-react-app which internally uses Babel for the JSX to JavaScript conversion.
Easy to do with es6 template strings (which allows line breaks). In your render method:
const css = ` .my-element { background-color: #f00; } ` return ( <div class="my-element"> <style>{css}</style> some content </div> )
As for use case I'm doing this for a div with some checkboxes that I'm using for debugging, that I would like to contain within one file for easy removal later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With