All you have to do to enable JSX transpilation in Meteor is add the jsx
package or the react
meta-package. Then, all files in your app with the .jsx
extension will be transpiled automatically and loaded on the server and the client according to Meteor's file loading rules.
JSX is an extension to JavaScript that lets you write snippets of XML-like markup directly in your JavaScript, that then compile into regular JavaScript function calls. Consider the following examples, taken from the React homepage.
JSX before compilation:
var HelloMessage = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
React.render(<HelloMessage name="John" />, mountNode);
JavaScript after compilation:
var HelloMessage = React.createClass({displayName: "HelloMessage",
render: function() {
return React.createElement("div", null, "Hello ", this.props.name);
}
});
React.render(React.createElement(HelloMessage, {name: "John"}), mountNode);
As you can see, JSX lets you write code that looks more like HTML but is actually JavaScript, reducing the need for a completely separate templating language.
The JSX transpilation also includes some commonly-used future JavaScript features. We have picked the same ones as React Native, listed below:
ES5
promise.catch(function() { });
ES6
<C onPress={() => this.setState({pressed: true})}
Math.max(...array);
class C extends React.Component { render() { return <View />; } }
var {isActive, style} = this.props;
var key = 'abc'; var obj = {[key]: 10};
var obj = { method() { return 10; } };
var name = 'vjeux'; var obj = { name };
function(type, ...args) { }
function(a, b = 1) { }
var who = 'world'; var str = `Hello ${who}`;
ES7
var extended = { ...obj, a: 10 };
function f(a, b, c,) { }