Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to embed literal <code> snippet within React/JSX component?

Tags:

reactjs

I'm new to React, and I'm trying to render a static CSS code sample within a React component, like this:

React.createClass({
  render: function() {
    return (
      <code> body { color: blue; } </code>
    )
  }
});

However, this does not seem to work.

Is there some special way for me to escape code like this, or am I doing something wrong?

like image 634
Austin York Avatar asked Dec 14 '22 15:12

Austin York


2 Answers

@ColonelThirtyTwo's answer does the trick:

<code>{"body { color: blue; }"}</code>
like image 182
Austin York Avatar answered Mar 03 '23 23:03

Austin York


It is probably also worth noting that this can be done with multiline strings too:

<code>{`
  html { background: red; }
  body { color: blue; }
`}</code>
like image 35
lukejacksonn Avatar answered Mar 03 '23 23:03

lukejacksonn