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:
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.
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:
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:
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:
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 along with examples respectively. You may also have a look at the following articles to learn more –