Updated June 14, 2023
Introduction to React JSX
In react js, we often face situations where we are required to combine JavaScript with html. React introduces the file type, which is JSX. Instead of creating a different technology for handling two in one place, they introduced the JSX. In react js, you will see states, functions, and HTML and CSS on the same file in any particular component. So to deal with all these attributes, they introduced the concept of a JSD file. For example, const x =<h1>How are you</h1>, so here we can see it is not complete html and not even JavaScript.
Uses of JSX in React
Given below are the uses of JSX in React:
1. Easy to write code
JSX allows us to write the HTML, CSS, and javascript on the same file, giving a better clarity and understanding of the code. We look at the example const x =<h1>Hello friend</h1>; in this, we have written the HTML and javascript on the same file; it does not look 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
Suppose you have worked on any plain javascript files with extensions like .js. In that case, you will see that you will have to use double quotes in case you want to combine the html with the javascript, and in the same way, if we’re going to display the javascript inside the html file with a .html extension then also we need to use javascript start and end tags. With the help of the JSX, we can 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 a combination of both javascript and HTML, and the html tag can be easily placed to the html real node without using the concept of the createElement().
How to Use JSX in React?
Below, we have given some important examples where we display the various uses and ways of working the jsx in the react js. In the example below, we combine the javascript, HTML, and css, which are all possible because of the jsx file type.
Here in the example, we can understand the importance of the jsx file, which gives us the flexibility to use all types of extensions in the same file. It reduces our effort to manage the separate flow for html and javascript. If we wanted to run the below example, we can create any file with the extension of jsx and place the examples on the files and see the output of the execution on the browser.
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 whole react jsx file output
<div id="main"></div>
Output:
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 whole react jsx file output
<div id="main"></app>
Output:
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 whole react jsx file output
<div id="main"></app>
Output:
Conclusion
From this tutorial, we saw the basic concept of the jsx and the uses and flow of the jsx; we saw its importance and uses with the help of some important examples, focusing the real-time cases.
Recommended Articles
We hope that this EDUCBA information on “React JSX” was beneficial to you. You can view EDUCBA’s recommended articles for more information.