Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tag Error: React JSX Style Tag Error on Render

Tags:

css

reactjs

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?

like image 622
user48545 Avatar asked Dec 17 '14 16:12

user48545


People also ask

Can I use style tag in React?

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.

Can I use style in JSX?

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.

What is render JSX in browser?

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.


1 Answers

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.

like image 88
Sebastian Avatar answered Sep 22 '22 17:09

Sebastian