-
Book Overview & Buying
-
Table Of Contents
Learn React with TypeScript - Second Edition
By :
JSX is the syntax we use in a React component to define what the component should display. JSX stands for JavaScript XML, which starts to give us a clue as to what it is. We will start to learn about JSX in this section and write some JSX in an online playground.
The following code snippet is a React component with its JSX highlighted:
function App() {
return (
<div className="App">
<Alert type="information" heading="Success">
Everything is really good!
</Alert>
</div>
);
}
You can see that JSX looks a bit like HTML. However, it isn’t HTML because an HTML div element doesn’t contain a className attribute, and there is no such element name as Alert. The JSX is also embedded directly within a JavaScript function, which is a little strange because a script element is normally used to place JavaScript inside HTML.
JSX is a JavaScript syntax extension. This means that it doesn’t execute directly in the browser – it needs to be transpiled to JavaScript first. A popular tool that can transpile JSX is called Babel.
Carry out the following steps to write your first piece of JSX in the Babel playground:
<span>Oh no!</span>
The following appears in the right-hand pane, which is what our JSX has compiled down to:
React.createElement("span", null, "Oh no!");
We can see that it compiles down to a React.createElement function call, which has three parameters:
"span"), a React component type, or a React fragment type.null is passed because there are no properties.Note
The right-hand panel may also contain a "use strict" statement at the top to specify that the JavaScript will be run in strict mode. Strict mode is where the JavaScript engine throws an error when it encounters problematic code rather than ignoring it. See the following link for more information on the strict mode in JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode.
You may also see /*#__PURE__*/ comments in the right-hand panel. These comments help bundlers such as webpack remove redundant code in the bundling process. We will learn about webpack in Chapter 3, Setting up React and TypeScript.
div element around the span element, as shown in the following code snippet:<div className="title">
<span>Oh no!</span>
</div>
This now transpiles to two function calls to React.createElement, with span being passed in as a child to div:
React.createElement("div", {
className: "title"
}, React.createElement("span", null, "Oh no!"));
We can also see a className property, with the "title" value passed with the div element.
Note
We have seen that React uses a className attribute rather than class for CSS class references. This is because class is a keyword in JavaScript, and using that would cause an error.
const title = "Oh no!";
<div className="title">
<span>{title}</span></div>
We declared a title JavaScript variable, assigned it "Oh no!", and embedded it within the span element.
Notice that the title variable is placed in curly braces inside the element. Any piece of JavaScript can be embedded within JSX by surrounding it in curly braces.
Our code now transpiles to the following:
const title = "Oh no!";
React.createElement("div", {
className: "title"
}, React.createElement("span", null, title));
span element. Add the following ternary expression:const title = "Oh no!";
<div className="title">
<span>{title ? title : "Something important"}</span></div>
A ternary expression is an inline conditional statement in JavaScript. The expression starts with the condition followed by ?, then what returns when the condition is true followed by :, and finally, what returns when the condition is false. For more information on ternary expressions, see the following link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator.
We see that the nested call to React.createElement uses the ternary expression as the child of span:
React.createElement( "span", null, title ? title : "Something important" );
This completes our exploration of JSX in the Babel playground.
In summary, JSX can be thought of as a mix of HTML and JavaScript to specify the output of a React component. JSX needs to be transpiled into JavaScript using a tool such as Babel. For more information on JSX, see the following link: https://reactjs.org/docs/introducing-jsx.html.
Now that we understand a little more about JSX, we will create our first React component in the next section.