×

React JSX

React JSX

As we know, all of the React components include a render function. The Render function specifies the HTML output of a React component. JSX is an acronym of JavaScript extension that allows writing JavaScript code that looks like HTML. JSX is an HTML-like syntax used by react which extends the ECMAScript so that HTML-like syntax can co-exist with JavaScript/React code. The syntax used by the preprocessors (i.e., transpilers like Babel) for transforming HTML-like syntax into standard JavaScript objects that a JavaScript engine will parse.

It provides you to write HTML/XML-like structures (e.g., DOM-like tree structures) within the same file where you write your JavaScript code, and then preprocessor will transform these expressions into actual JavaScript code. JSX tags also have a tag name, attributes, and children just like XML/HTML.

Why use JSX?

  • It is faster in comparison to JavaScript as it performs optimization during translation of the code to JavaScript.
  • Instead, the separation of technologies by putting markup and logic in separate files, React has the components that contain both.
  • It is type-safe because most of the errors are found at compile time.
  • It makes template creation easy.

Nested Elements in JSX

If you want to use more than one element, you require wrapping it with one container element. Here, the div is used as a container element that has three nested elements in it.

App.JSX
 import React, { Component } from 'react';
 class App extends Component{
 render(){
 return(
 

Hello World !!

This is TutorialandExample

Website with the best tutorials.

) } } export default App;

Output:

React JSX

JSX Attributes

The attributes in JSX with the HTML elements are same as the regular HTML. The naming convention in JSX is in camelcase for attributes instead of standard naming convention of HTML like class in HTML becomes className in JSX. This is because the class is reserved keyword in JavaScript. There is also the use of custom attributes in JSX. We have to use data-prefix, for custom attributes.

The example given below shows the use of custom attribute data-customAttr as an attribute for the <p> tag.

For example:

import React, { Component } from 'react';
 class App extends Component{
render(){
return(

Hello World !!

This is TutorialandExample

Website with the best tutorials.

); } } export default App;
JavaScript Expressions

JavaScript Expressions

The expressions of JavaScript can be used inside of JSX. We have to wrap it with curly brackets {}.

The following example shows it more clearly.

Example:

import React, { Component } from 'react';
class App extends Component{
render(){
return(

{12+28}

Hello World !!

This is TutorialandExample

Website with the best tutorials.

); } } export default App;

Output:

Hello world

There is not any use of if else statements inside JSX, we have to use conditional (ternary) expressions. In the following example, the value of variable x equals to 1 so the browser will give true, if we replace this value with some other value, it will give false.

For Example:

import React, { Component } from 'react'; 
class App extends Component{
render(){ 
var x=1;
return(

{x == 1 ? 'True!' : 'False'}

This is TutorialandExample

Website with the best tutorials.

); } } export default App;

Output:

so the browser will give true

JSX styling

React recommends using inline styles. To set inline styles, we require camelCase syntax. React automatically appends px after the number value on specific elements.

The following example shows you the styling in JSX:

For example:

import React, { Component } from 'react'; 
class App extends Component{ 
render(){ 
var newStyle = {
fontSize: 90,
fontFamily: 'Times New Roman',
color: '#789067' 
} 
return(  

www.google.com

This is TutorialandExample

Website with the best tutorials.

); } } export default App;

Output:

JSX styling

JSX Comments

For writing comments, we have to put curly braces {}. The comments in JSX are begin with /* and ends with */ as in the case of JSX expressions. Below example clarifies how to use comments in JSX.

For example:

import React, { Component } from 'react';
class App extends Component{ 
render(){ 
var newStyle = {fontSize: 90, 
fontFamily: 'Times New Roman', 
color: '#789067'
}  
return( 

www.google.com

This is TutorialandExample

Website with the best tutorials.

{/* this is a comment of JSX*/
); } } export default App;

Related Topics

React Events

React Events An event is an action that triggers a response of the user action or system-generated event. As HTML, React can also perform actions based on user events. React has the...

2 minutes read.

React Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.

React Components

React Components Former, the developers used to write more than thousands of lines of code to develop a single-page application. These applications follow the structure of traditional DOM, and it was very challenging to make...

2 minutes read.

Features of ReactJS

ReactJS Features As of now, within the developers, ReactJS is gaining much popularity as the best JavaScript framework. It is crucial for the front-end ecosystem. There are some of the crucial features of ReactJS that are...

2 minutes read.

React CSS

What is React CSS React CSS is used to provide the style to the React applications. The style attribute adds dynamically-computed styles at render time, and it is one of the most used...

5 minutes read.

Difference Between State and Props in React

Comparison between State and Props State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over...

2 minutes read.

React Keys

React Keys A key can be defined as a unique identifier. In React, it is used for identifying the items that have changed, deleted, and updated from the lists. React Keys are helpful...

3 minutes read.

React State

React State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over time. The change...

3 minutes read.

React Higher-Order Components

React Higher-Order Components In short form, Higher-Order Components are represented as HOC. The Higher-order component is an advanced technique to use component logic. HOC is a function that takes the component and returns the new...

2 minutes read.

How to use React Context

React Context React Context is used to pass the data through the component tree without passing the props down manually at the every level. In React Application, the data is passed in a top-down approach...

4 minutes read.

ReactJS Tutorial

ReactJS Introduction React Tutorial helps you to understand the basic and some advanced concepts of ReactJS. As of now, it is the essential front-end library that is developed by a...

3 minutes read.

React Hooks

React Hooks The Hooks were introduced in React 16.8. By using React Hooks, you can use the state and other features of the React without writing a class. Hooks do not work within the classes....

5 minutes read.

React Code Splitting

React Code Splitting React Code Splitting is the procedure of splitting the bundle files by which the files get easily loaded on the webpage. The react application bundles the files by using...

4 minutes read.

React Forms

React Forms Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the...

3 minutes read.

React Fragments

React Fragments The fragments in React are introduced from the 16.2 and above versions. Fragments allow you to group a list of child elements without adding any extra node in the DOM. In React,...

2 minutes read.

React Constructors

What is constructor? The constructor is a method that is used for initializing the state of the object in the class. It calls automatically during the creation of an object in...

4 minutes read.

React Native vs ReactJS

Difference between React Native and ReactJS React Native React Native is also an open-source JavaScript framework that is used for the development of a mobile application for iOS Android and Windows. It...

6 minutes read.

React Environment Setup Step by Step

React Environment Setup This section will provide you the information about how to set up an environment for the successful development of ReactJS application: Pre-Requisite for ReactJS NodeJS and NPMReact and React DOMWebpackBabel There are two...

5 minutes read.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

2 minutes read.

Pros and Cons of ReactJS

Pros and Cons of ReactJS There are various advantages, and disadvantages of ReactJS are as follows: Benefits of ReactJS 1. Easy to Learn and Use: ReactJS is very easy to use and learn. It has a good...

3 minutes read.