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?
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.
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.
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.
Compilation in ReactAt build time the JSX is compiled into imperative JavaScript code. At runtime this code is loaded into the browser and executed.
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>
);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With