This is the third article in ReactJS – Zero to Hero Series. I will recommend reading the first two articles (link given below), before going through this article.

In this article, we will see what is JSX and its features. We will also see how to Render Elements in React.

JSX is an external, domain-specific language that is optimized for generating XML-like documents. For a React application, JSX will typically be used to generate HTML, but it also supports custom React components and SVG.

A very simple JSX expression is shown below. The statement is neither a string nor HTML.

  1. const element = <h1>Hello, world!</h1>;

JSX Attribute Expressions (Specifying Attributes with JSX)

To supply props to a React component, we specify attributes on the JSX element. Consider the below JSX expression.

React

When the component is rendered, the JavaScript expression will be evaluated and supplied as the value for the new prop on the Hello component. JSX attributes can also be given a literal string by using quotes instead of curly braces.

  1. const element = <div tabIndex="0"></div>;

Child Expressions (Specifying Children with JSX)

JSX is hierarchical, so any valid JSX expression may be a child of any other JSX expression. We can have many JSX expressions, or elements, within a single JSX component. That is, a JSX expression can have multiple direct children.

Within components, you can reference JSX child elements via the special property – this.props.children

  1. const element = (
  2. <div>
  3. <h1>Hello!</h1>
  4. <h2>Good to see you here.</h2>
  5. </div>
  6. );

JSX Represents Objects

The JavaScript compiler in React compiles React calls to React.createElement() calls. So, the below two code snippets are identical.

  1. const element = (
  2. <h1 className="greeting">
  3. Hello, world!
  4. </h1>
  5. );
  6. const element = React.createElement(
  7. 'h1',
  8. {className: 'greeting'},
  9. 'Hello, world!'
  10. );

HTML Attributes

The two curly braces are because the outer curly braces delimit the JSX expression as usual and the inner curly braces delimit the JavaScript object literal.

Another way of writing the above code is to extract the objects literal definition into a variable.

React

This makes it easier to see where the style object literal is defined and how it is assigned to the style attribute.

Rendering Elements

Consider the below ticking clock example.

  1. function tick() {
  2. const element = (
  3. <div>
  4. <h1>Hello, world!</h1>
  5. <h2>It is {new Date().toLocaleTimeString()}.</h2>
  6. </div>
  7. );
  8. ReactDOM.render(
  9. element,
  10. document.getElementById('impl')
  11. );
  12. }
  13. setInterval(tick, 1000);

In the above example, the application calls “ReactDOM.render()” every second from a “setInterval” callback. React DOM compares the element and its children to the earlier one and applies the DOM updates based on the requirement to bring the DOM to the required state.

Summary

In this article, we saw the introduction to JSX, its features, and the way of rendering elements in React.