Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSX compiler output is not valid JavaScript

I'm playing with React and I've run into a problem with the offline JSX compiler.

Here's my JSX code in a file named helloworld.js, placed in a folder named src within the root directory of my project:

/** @jsx React.DOM */

var Hello = React.createClass({
    render: function () {
        return <div>
                <h3>Hello, {this.props.name}</h3>
            </div>
    }
});

React.renderComponent(
    <Hello name={"Jane Doe"} />,
    document.getElementById('example')
);

In the command line, when I navigate to the root directory of my project and I run this:

jsx /src /build

The output file helloworld.js is produced in the build folder but it does not contain valid JavaScript.

Here's how the content looks like:

/** @jsx React.DOM */

var Hello = React.createClass({displayName: "Hello",
    render: function () {
        return <div>
                <h3>Hello, {this.props.name}</h3>
            </div>
    }
});

React.renderComponent(
    <Hello name={"Jane Doe"} />,
    document.getElementById('example')
);

As you can see, it still contains the inline HTML instead of valid JavaScript. There is no indication of an error in the command line. It looks like this:

built Module("helloworld")
["helloworld"] 

Does anyone have an idea why that might be happening?

like image 698
Marta Ciesielska Avatar asked Mar 09 '15 12:03

Marta Ciesielska


People also ask

How do you write JavaScript in JSX?

To add JavaScript code inside JSX, we need to write it in curly brackets like this: const App = () => { const number = 10; return ( <div> <p>Number: {number}</p> </div> ); }; Here's a Code Sandbox Demo. Inside the curly brackets we can only write an expression that evaluates to some value.

What browsers can not read JSX?

Browsers can't read JSX because there is no inherent implementation for the browser engines to read and understand them. JSX is not intended to be implemented by the engines or browsers, it is intended to be used by various transpilers to transform these JSX into valid JavaScript code.

What is JSX compiler?

JSX stands for JavaScript syntax extension. It is a JavaScript extension that allows us to describe React's object tree using a syntax that resembles that of an HTML template. It is just an XML-like extension that allows us to write JavaScript that looks like markup and have it returned from a component.

Is JSX compiled or interpreted?

Compilation in ReactAt build time the JSX is compiled into imperative JavaScript code. At runtime this code is loaded into the browser and executed.


1 Answers

Maybe try to wrap your html in brackets.

var Hello = React.createClass({displayName: "Hello",
    render: function () {
        return (
            <div>
                <h3>Hello, {this.props.name}</h3>
            </div>
        );
    }
});
like image 145
deowk Avatar answered Oct 16 '22 15:10

deowk