Formatting code with tag in React and JSX

Use template literals

Template literals allow the use of multi-line strings which preserve leading/trailing white-space and new lines.

const pythonCode = `
    print(
        "Hello, World!"
    )
`

class PreFormattedCode extends React.Component {
    render() {
      return <React.Fragment>{pythonCode}</React.Fragment>
    }
}

ReactDOM.render(<PreFormattedCode />, document.getElementById('code'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<!-- The content rendered into this tag will preserve whitespace -->
<pre id="code"></pre>

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)