2

I want to show javascript code as plain text inside the render function. Whats the best way, to prevent that the code will be executed? In HTML i can use code and pre.

render() {
    return  <code>
                var path = require('path');
                var config = {
                    entry: path.resolve(__dirname, 'app/main.js'),
                    output: {
                        path: path.resolve(__dirname, 'build'),
                        filename: 'bundle.js'
                    },
                module: {
                    loaders: [{enter code here
                        test: /\.jsx?$/,
                        loader: "babel-loader",
                        query: {
                            presets: ["es2015", "react"]
                        }
                    }, {
                        test: /\.css$/,
                        loader: 'style!css'
                    }, {
                        test: /\.(png|jpg)$/,
                        loader: 'url'
                    }, {
                        test: /\.(eot|woff|ttf|svg)$/,
                        loader: 'url?limit=100000'
                    }]
                 }
              };

             module.exports = config;

            </code>
}

3 Answers 3

1

In ES6 you can use template strings and you don't need to escape your code:

render() {
    return  <code> {`
                var path = require('path');
                var config = {
                    entry: path.resolve(__dirname, 'app/main.js'),
                    output: {
                        path: path.resolve(__dirname, 'build'),
                        filename: 'bundle.js'
                    },
                module: {
                    loaders: [{enter code here
                        test: /\.jsx?$/,
                        loader: "babel-loader",
                        query: {
                            presets: ["es2015", "react"]
                        }
                    }, {
                        test: /\.css$/,
                        loader: 'style!css'
                    }, {
                        test: /\.(png|jpg)$/,
                        loader: 'url'
                    }, {
                        test: /\.(eot|woff|ttf|svg)$/,
                        loader: 'url?limit=100000'
                    }]
                 }
              };

             module.exports = config;
            `}
            </code>
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would wrap the code into a custom script tag and load the code when needed. This prevents possible expression interpolation in the template string approach.

<script type="raw_js" id="raw-js">
    // your js code here, which needs to display
</script>

load:

render: function() {
    var code = document.getElementById('raw-js').innerHTML;
    return <code><pre>{code}</pre></code>;
}

See the sample at jsFiddle.

Comments

0

You can return it as a string by putting quotes around it like this:

render() {
    return  "
                var path = require('path');
                var config = {
                    entry: path.resolve(__dirname, 'app/main.js'),
                    output: {
                        path: path.resolve(__dirname, 'build'),
                        filename: 'bundle.js'
                    },
                module: {
                    loaders: [{enter code here
                        test: /\.jsx?$/,
                        loader: \"babel-loader\",
                        query: {
                            presets: [\"es2015\", \"react\"]
                        }
                    }, {
                        test: /\.css$/,
                        loader: 'style!css'
                    }, {
                        test: /\.(png|jpg)$/,
                        loader: 'url'
                    }, {
                        test: /\.(eot|woff|ttf|svg)$/,
                        loader: 'url?limit=100000'
                    }]
                 }
              };

             module.exports = config;

            ";
}

Make sure you escape all quotes used in the code with a . (I did it for you in my example)

Comments

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.