Getting Started with JSX in React
In this article we will take a look at the JSX—an HTML-like syntax used by React to declare user interfaces. By the end of this post, you will have a better understanding of what JSX is, how it works, and why it's a preferred way to declare user interfaces in React.Let's start with an example: We start by importing React APIs from react and react-dom packages. We'll use these APIs later. Next, we declare the header React element. This will be an <h1> element. After that, we get the DOM node by its id. Finally, we ask React to render the header into the root DOM node with the ReactDom.render() function. We provide a React element to render and an HTML element that will be used as a container. The syntax we've used to declare the header is JSX—a special syntax used by React to declare UI. It intentionally looks like HTML to make it easier to work with UI elements and layout. As with HTML we can define attributes, which are called properties in React: Here we've got the same <h1> element with id and contentEditable properties defined. We can use strings and JavaScript expressions as values. In case of expressions, we wrap an expression with curly braces. Also, we can define children elements with JSX: Here we've got a <div> element that contains <h1> and <p> elements. If we'll try to run a code that uses JSX directly in a browser, we'd get an error message. This is because JSX is not a valid JavaScript syntax. We need to process this code first. We can use Babel or TypeScript for this. They will convert the JSX syntax into a plain JavaScript code. Let's take a look at the code before and after such conversion: Here we can see that our JSX declaration has been converted into the React.createElement() function call. This function takes three parameters. The first parameter defines a type of element to create, the second one is for properties, and the third one allows to specify children for the component. This function returns an object that is later used by React to render actual DOM nodes. As we've seen, JSX is a special syntax that got converted to React.createElement calls. So why use JSX instead of calling the createElement function directly ? To answer that, let's look at the following code: We can tell what happens here, but this requires reading through the code. Now, let's take a look at the same logic written with JSX: Here we can understand the structure after a glance. JSX is easy to read and reason about. That's the main feature of JSX that makes it preferable over the plain JavaScript. In this article, we've seen how to use JSX to declare React components. JSX makes it easier to declare your components and define UI of your application. It looks like HTML and got converted to pure JavaScript by Babel or TypeScript. When your React application runs in a browser, the browser knows nothing about JSX and works with JavaScript only. Here are additional resources that will help you to learn more about JSX and React: