EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

React JSX

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » React Native Tutorial » React JSX

React JSX

Introduction to React JSX

In react js many time we face situation where we required to combined the JavaScript with html react introduces the file type which is JSX. Instead of creating a different technology for handling two at one place they introduced the JSX. You will see in the react js there will be states, there will be functions and there will be HTML and CSS also on the same file in any particular component. So to deal with all these kind of attributes they introduced the concept of JSD file. For example const x =<h1>How are you</h1>, so here we can see it not complete html and not even JavaScript.

Uses of JSX in React

Given below are the uses of JSX in React:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. Easy to write code

In case of JSX it allows us to write the HTML, CSS and javascript on the same file which gives a better clarity and understanding for the code. We take a look on the example like const x =<h1>Hello friend</h1>, in this we have written the HTML and javascript on the same file , it does not looks like a complete HTML or any complete javascript. We can also attach the css along with the html like const x =<h1 style={StyleObject}>hello friend</h1>, here in this we can add the style object for StyleObject with some css attributes which can change the design of the h1 tag.

2. Used without “ ” with JavaScript

If you have worked on any plain javascript files with extension like .js then you will see that you will have to use double quotes in case if you wanted to combined the html with the javascript and in the same way if we wanted to display the javascript inside the html file with .html extension then also we need to use javascript start and end tags. With the help of the JSX we will be able to simply write without these things and we can easily manage both the flow without using the quotes and with any javascript tag for managing html and javascript respectively.

3. Without createElement() place the HTML in JavaScript

With the help of jsx we will be able to create html dom from javascript into the html, which means we will write the HTML inside the javascript combination with the javascript and css and that can be directly converted to html dom without using the attribute or function called createElement(). It saves the extra calculation because we are not again creating the dom from javascript to another format. For example cons x =<h1>Hello friend </h1>, here it is combination of both javascript and html and the html tag can be easily be placed to the html real node without using the concept of the createElement().

How to Use JSX in React?

In the below we have given some of the important examples where we are displaying the various uses and way of working of the jsx in the react js. In the example below we are combining the javascript, HTML and css all together and all these are possible because of the jsx file type.

Here in the example we can understand the importance of the jsx file which giving us a flexibility to use all type of extensions in the same file. It is reducing our effort to manage separate flow for html and for javascript. In case we wanted to run the below example then we can create any file with the extension of jsx and we can place the examples on the files and we can see the output of the execution on the browser.

Popular Course in this category
All in One Software Development Bundle (600+ Courses, 50+ projects)600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (3,144 ratings)
Course Price

View Course

Related Courses

Example #1

Code:

function fullName(userDetail) {
return userDetail.userFirstName + ' ' + userDetail.userMiddleName + ' '+userDetail.userLastName;
}
//Defining the css to design the output view with color and with some other attributes.
constuserStyle={
color:"red",
fontSize:"15px"
}
//Defining the css to design the output view with color and with some other attributes.
constuserdetails = {
userFirstName: 'Ranjan',
userMiddleName: 'Kumar',
userLastName : "Pandey"
};
//Here we can see that we are combining the html and javascripttogether .
const user = <h1 style ={userStyle}>Welcome to React programing, {fullName(userdetails)}!</h1>;
ReactDOM.render(user, document.getElementById('main'));
HTML,
Below is the html which will hold the hole react jsx file output
<div id="main"></div>

Output:

React JSX 1

Example #2

Code:

//Defining the css to design the output view with color and with some other attributes.
constjsxStyle={
color:"red",
fontSize:"15px",
justifyContent: "center",
alignItems: "center",
textAlign :"center",
marginTop: "1em"
}
class JsxExample extends React.Component {
render() {
return <div>
//Here we can see that we are combining the html and javascript together.
<h1 style={jsxStyle}>This is the tutorial for JSX</h1>
</div>;
}
}
React.render(<JsxExample />, document.getElementById('main'));
HTML,
Below is the html which will hold the hole react jsx file output
<div id="main"></app>

Output:

This is the tutorial

Example #3

Code:

//Defining the css to design the output view with color and with some other attributes.
var Shape = React.createClass({
shapeStyle: {
width: "101px",
height: "101px",
mozBorderRadius: "51%",
alignItems: "center",
webkitBorderRadius: "101%",
background: "green"
},
render: function() {
return (
//Here we can see that we are combining the html and javascript together.
<div className="shape-class" style = {this.shapeStyle}></div>
);
}
});
React.render(
<Shape />,
document.getElementById('main'));
HTML,
Below is the html which will hold the hole react jsx file output
<div id="main"></app>

Output:

React JSX 3

Conclusion

From this tutorial we saw the basic concept of the jsx and we saw the uses and the flow of the jsx, we saw its importance and uses with the help of some important examples, with focusing the real time cases.

Recommended Articles

This is a guide to React JSX. Here we discuss the introduction, uses of JSX in react and how to use JSX in react? with examples respectively. You may also have a look at the following articles to learn more –

  1. React Router Transition
  2. React Redux Connect
  3. React Native Template
  4. React-Native StyleSheet

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
React Native Tutorial
  • Basic
    • What is React Native?
    • React Versions
    • React Constructor
    • React Native Architecture
    • React Native Libraries
    • React Components Libraries
    • React Native Components
    • React Component Library
    • React Component Lifecycle
    • React Native Framework
    • React Higher Order Component
    • React Ternary Operator
    • React Native Charts
    • React Native Layout
    • React Native Grid
    • React Native Fetch
    • React Native Modal
    • React Native SVG
    • Button in React Native
    • React List Components
    • React Native Element
    • React Native FlatList
    • React Native SectionList
    • react native dropdown
    • React Native Menu
    • React Native State
    • React State Management
    • React Native Tabs
    • React Native Tab Bar
    • React Format
    • React-Native Switch
    • React Native Firebase
    • React Native Flexbox
    • React Native StatusBar
    • React Native ScrollView
    • React Native ListView
    • React Native TextInput
    • React Native Table
    • React-Native Border Style
    • React Native Search Bar
    • React-Native StyleSheet
    • React Native Vector Icons
    • React Native Login Screen
    • React Native Splash Screen
    • React Native Image Picker
    • React Native Navigation
    • React Native Swift
    • React Controlled Input
    • React Fragment
    • React Native Color
    • React Portals
    • React Refs
    • React shouldComponentUpdate()
    • React ComponentDidMount()
    • React componentWillUpdate()
    • React Native Orientation
    • React Native Animation
    • React Native Form
    • React Props
    • React Native Linear Gradient
    • React Native AsyncStorage
    • React Error Boundaries
    • React Native Progress Bar
    • React-Native Calendar
    • React Native Linking
    • React Native DatePicker
    • React Native Image
    • React Native Drawer
    • React Native Drawer Navigation
    • React Native Fonts
    • React Native Overlay
    • React Native OneSignal
    • React Native Template
    • React Native Router
    • React Router Transition
    • React Dispatcher
    • React Native Redux
    • React JSX
    • React native keyboardavoidingview
    • React Native Permissions
    • React Redux Connect
    • React Native Material
    • React Native Gesture Handler
    • React Native Theme
    • React Native Accessibility
    • React Native Justify Content
    • MobX React Native
    • React Native Authentication
    • React Native UUID
    • React Native Geolocation
    • React Native Debugger
    • React Native Carousel
    • React Native Local Storage
    • React Native TypeScript
    • React Bootstrap
    • React Native SQLite
    • React Native Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More