Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

React JSX – JSX in React

React JSX is the React JavaScript extension, or you can say react JavaScript XML, both are same. So the question is what is React JSX? And why need to know about it?

React’s JavaScript Extension JSX (JavaScript Extension) allows you to write JavaScript code that looks similar to HTML. JSX, an HTML-like syntax that React uses to extend ECMAScript, allows the syntax to co-exist with JavaScript/React. The syntax is preprocessors, i.e., source to source compiler such as babel, to convert HTML-like syntax into a standard JavaScript object that a JavaScript engine can parse.

JSX allows you to create HTML/XML-like structures, such as DOM-like tree structures, in the same file you write JavaScript codes. The preprocessor will then transform these expressions into actual JavaScript Code. JSX tags are similar to XML/HTML in that they have a tag name and attributes.

I know it just looking somewhat confusing, So let’s take an example to understand about it:

App.js

import React, { Component } from 'react';  
class App extends Component{  
   render(){  
      return(  
         <div>  
            <h1>Hello World!!!!</h1>  
         </div>  
      );  
   }  
}  
export default App;

So we have here imported react and component, we need to import these packages to work in react, and we have to extend app class with component, so whatever we write here add to class file as we talk in previous tutorial. Now for render function, this will be used to show the data or whatever you write in this will be show on in the browser. In render function we used div, in this div we added a h1 tag similar to the HTML, so the output we get is:

react jsx

Looks similar to HTML, right? So we have used 2 HTML tags, these tags are the React JSX. To check it you just need to comment the line in which we have imported the React in index.js file to remember that, then the output will be:

react jsx

In this, you can see the error react is not defined, hence you can’t use JSX.

Why use React JSX?

  • Because it optimizes the code while translating it to JavaScript, it is much faster than regular JavaScript.
  • React uses logic and markup in one file instead of separating technologies. In a subsequent section, we will cover components.
  • It is type-safe, and most errors can be detected at compilation time.
  • It is easier to create templates.

Nested Elements in JSX

You need to wrap more than one element with one container element. This div container element has three nested elements.

App.js

import React, { Component } from 'react';  
class App extends Component{  
   render(){  
      return(  
         <div>  
            <h1>Hello World!!!</h1>  
          <h2>Learn in easy way</h2>  
            <p>This website contains the best React tutorials.</p>  
         </div>  
      );  
   }  
}  
export default App;

Output

react jsx

Attributes in JSX

We can also use custom HTML attributes and properties and standard HTML properties. Data prefix is required when adding custom attributes. We have added data-attribute, As an attribute of the p element in this example:

App.js

import React from 'react';

class App extends React.Component 
{
   render() {
      return (
         <div>
            <h2>This is heading 1</h2>
            <h3>This is heading 2</h3>
            <p data-attribute = "firstattr">This is a Paragraph!!!</p>
         </div>
      );
   }
}
export default App;

Styling in JSX

App.js

import React from 'react';

class App extends React.Component {
   render() {
      var heading = {
        fontSize: 130,
         color: 'blue'
      }
      return (
         <div>
            <h2 style = {heading}>React Guru</h2>
         </div>
      );
   }
}
export default App;

React recommends using inline styles. We must use the camelCase syntax whenever we need to set up inline types. The above example illustrates how to add heading to element the h1.

Output

react jsx

JSX Comments

App.js

import React, { Component } from 'react';  
class App extends Component{  
   render(){  
      return(  
         <div>  
            <h1 className = "hello" >Hello World!!!</h1>  
        {/* This is a comment in JSX */}   
         </div>  
      );  
   }  
}  
export default App;

React JSX allows users to use remarks that start with /* and end with or */ and wrap them {in curly braces with curly braces, similar to the example for JSX expressions. This example illustrates how to make use of comments within JSX.

JSX Expressions

Inside JSX, JavaScript expressions can be utilized. You just wrap it up {in curly brackets}. This example illustrates rendering 2.

import React from 'react';

class App extends React.Component 
{
   render() {
      return (
         <div>
            <h1>{ 10 + 20 }</h1>
         </div>
      );
   }
}
export default App;

Output

react jsx

We aren’t able to utilize the conditional (ternary) expressions when other statements are in JSX instead. In the following example variable I is 1 thus the browser renders true. However, it’ll be false if we alter the value to another one.

import React from 'react';

class App extends React.Component 
{
   render() {
      var i = 4;
      return (
         <div>
            <h1>{i == 7 ? 'True!' : 'False'}</h1>
         </div>
      );
   }
}
export default App;

Output

react jsx
Reactjs Guru
Reactjs Guru

Welcome to React Guru, your ultimate destination for all things React.js! Whether you're a beginner taking your first steps or an experienced developer seeking advanced insights.

React tips & tutorials delivered to your inbox

Don't miss out on the latest insights, tutorials, and updates from the world of ReactJs! Our newsletter delivers valuable content directly to your inbox, keeping you informed and inspired.

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents