Lesson finished
Unlock all 45 lessons of React.jsFull course ₹40,000 · paid
JSX Explained: Expressions, Fragments and Conditional Rendering
A free lesson from Skill-Lync's React.js course, on Grow by Skill-Lync · Last updated 16 Nov 2026 · Session: “Week 2 Session 1”
What will you learn in this free React.js lesson?
After a recap of week one and a tour of the official React documentation, the instructor explains what JSX is and why Babel must transpile it. Demos cover curly-brace expressions, the single-root rule, empty fragment tags, conditional rendering, JSX in attributes, and the React.createElement output.
- What JSX is and why React uses it
- Embedding JavaScript expressions in curly braces
- Single root element and empty fragment tags
- Conditional JSX and JSX inside attributes
What comes after lesson 3?
Later in Skill-Lync's React.js course (paid): lesson 4, Week 2 Session 2; lesson 5, Week 3 Session 1; lesson 6, Week 3 Session 2. The full course has 45 lessons (20 h 46 min of video); see the course overview or its price and syllabus.
Lesson transcript
Machine transcript of the lesson audio (lightly cleaned). Technical terms may contain errors; the video is the reference.
Hi students. So welcome to week two web development with ReactJS. On this week, it's time for core concepts. So let's jump into the core concepts of ReactJS. So let's quickly recap about the things which we acquired last week. So in previous week, we learned about prerequisites and essential software and editors to develop the code using the ReactJS. So in that prerequisites, we talked about it's important to understand HTML, CSS, and JavaScript well. Along with that, we had a word together and discussed various aspects of ES6. And we have seen few examples where we can use ES6 and different constructs which got added in ES6 JavaScript.
Apart from that, we also touched based on different array methods. However, I didn't went in that detail. And I asked all the students to go over array methods so that we can use them effectively in React. And believe me, like we are using the array methods everywhere in ReactJS when we are basically listing something or creating a right side or left side navigation, listing our to-dos item in one of our project or something like that. We will use those array functions, built-in array functions a lot. So if you are not done that, please revisit Google or open a standard MDN documentation and work upon those array methods.
So that we discussed briefly. So that we discussed briefly. Then we also discussed about the difference between the ReactJS and other frameworks. And we also looked at the core concept of virtual DOM where React actually produces a copy of DOM in its own memory. And it keeps track of that structure or DOM, which I've created in memory for different timestamps. And if something changes, it's just a matter of comparing with one snapshot with the one which I've recorded for a previous timestamp. And then it figures out exact things which need to update to the DOM. And that is how it is very efficient with the traditional JavaScript browser used to rerender or browser used to recalculate entire DOM tree.
But the React basically avoids that and it just updates whatever needs to do. Then we went over the folder structure in the React scaffold and talked about the package.json, src folder, public folder, all those things. And we learned about the functional and class-based component. So that is what the summary of recap. And then what we are going to discuss this week. So menu of this week is JSX, props, state, set state, destructuring, props and state and event handling. Now event handling will be having a deep dive in next week.
But this week we will at least go over the basics of event handling so that we will get the gist of it. So that we are going to do. So long story short, let's start. So learning objectives. So what we are expecting by end of the session, you should be able to understand what is JSX, the difference between prop and state, what the rerendering of component means and how it can be done using set state. Then what is the destructuring of the props and state and then purpose of event handling.
So that is something which we are going to discuss. But before that, I would like you to revisit again React documentation. Let's spend some time and see how one can traverse between the different sections. And then we will start with our actual session. Okay. So this is a React documentation and I am again showing this in the first week. Just briefly went over it. But now let's go some on aspects of it in detail. So this is actual official documentation of React and over here it basically specifies the version.
And then you can change the version and basically track the change logs per version. So 18.1.0 is the current stable version. So it's good to use. And then there is a tech tutorial section. So if you open this in new tab. So this is tutorial in introduction. It's single stop tutorial where they are actually building a tic-tac-toe game. And based on that game, they are actually learning the React. They want people to learn the React. So it's a good thing to go over when you get a chance.
Because like some people prefer or some students prefer learning by doing. However, some and we also recommend that first understand concept and then jump into the coding. But this approach of having the tutorial by building something might suit a few folks. So I would recommend if you are one of those, then please check out this tutorial section. And then here is the main three points which are regarding React. First it is declarative. So whatever you need to do, you can declare. And it's very obvious. So that is what they mean by declarative.
It's a component base. It's very focused. It's focused on the component logic, which is getting written in a JavaScript instead of templates. Other frameworks like Angular. And then it's learn once and write anywhere. I mean, once you understand a React concept, then even you can write a React app for a mobile device or maybe for a desktop using Electron or React native for a mobile device. So the things are very easy once you learn a basic concept of it. So which is why they are saying like learn once and write anywhere.
So just below that they are providing a few quick examples like simple component. Well, I like how a class based component can be written. They have showed that then how to create a stateful component. This is something which we are going to cover everything we are going to cover. But I just wanted you to run by official document once before we jump into the details section. And then if you click to the docs, this is a very important part as far as my understanding. If you go to the docs, it basically presents you eight to nine sections.
And it's right from installation. Depending on your needs, you can choose your section and learn more about it. So installation is one which I am asking you guys to go over. But it will happen that you will not understand few things. But still, it's important to go over so that you can recall whenever you will need to use them or come across those concepts. So it's very important to read up on installation. The main concept is the one which is very important. And we are going to discuss on all these aspects one by one.
We already discussed Hello World. And this week, we are mostly looking into the JSX components, props, rendering elements, state, all those things. So till point five, we are anyways going to cover. But I would still strongly suggest that you guys go and check out these documentation properly. So that you can get a sense out of it. So there are a few online playgrounds which they are suggesting like code pane, code sandbox, stack please. And they have their dedicated template. It's good to have a quick if someone want to create a quick demo of something and share with someone.
So you can easily use this code pane, code sandbox, and stack please to demonstrate that. So it's pretty good. And you should check it out. And then if you're a designer, then what you need to do is what indicated over here. So it's pretty cool. And just check it out before you jump into the actual react word. Okay. So that is how I wanted to ensure that you guys are doing. And then there is a block sections react periodically updates this section. If there is a new version, which is coming in.
Now, this is recently updated on March 29, 2022 by the react team, because that is the time where react 18 available at NPM, which is load package manager. So what they're doing, what are the new things? What are the keynotes? So all those things they mentioned over here, it's a good, good resource to check out. There is something called as a community where they indicated, like if you stuck somewhere or if you are wanted to debug something there, how to go about it, how to check those things, how to resolve your own doubts.
What are the popular discussions for us? What are the popular discussions for us? And the news feeds that you can subscribe to. So all those resources are indicated over here. There are very community-driven development happening across the globe. So it's important to check these out. Then there are different meetups happens at different part of the world and you can be one of the candidates there and you can learn really a lot from there. Then there are good articles written by experts of the React and JavaScript and there are podcasts, videos and external resources. So a good amount of material is available over here. So you rather than just Googling a random website for getting answer of your solution first, try to visit this website and then if you figure out, okay, whatever you're looking for is not available.
Then start with the random Googling because whatever indicated over here is original and subpure and that is a correct thing to follow. So first look at this and then Google is always yours. Okay. Okay. Let's come back to the week agenda and let's start with first part, which is JSX. So JSX, what is JSX? We talked or we heard about this buzzword a lot in week one JSX, JSX, what the heck JSX is? So can you look at this code snippet in a black background? So const element equal to H1 hello, world and then closing of H1. Now this is neither a string because if it is a string, then it should be quoted with at least a single code or double code.
Codes are not there. So it's not a string and it's not HTML because how we can assign HTML to the element that to a JavaScript variable or a constant. So that is odd, right? Yes, it's neither string nor HTML tag. It's JSX. JSX is a combination of JavaScript and XML. Now what XML? Where that XML comes from? So HTML is a hypertext markup language, which is based upon XML standards. So XML basically brought up tags, a concept of tags, opening and closing and have a proper structure.
It come up with a DTD, which has the standards to validate XML. So that is what HTML's ancestor is XML. And that is the word which JSX picked from. So it's a JavaScript. So JSX from a JavaScript and X from the XML. So you can combine JavaScript and HTML or XML into single unit and then it becomes JSX. So it is JSX and it is a syntax expression to JavaScript. So it's a, what you can say, it's a teaching new skills to the existing JavaScript. Okay. We are teaching new skills, new tricks to the JavaScript. And that is what JSX is all about.
So why we are using a JSX over here? So JSX essentially a proprietary, which Facebook created, uh, basically for a React and JSX actually produces a React element. What are the React elements? We will look after that, but React element is essentially objects. Okay. And in order to create objects, there is a React specific API, which React team provided us. Where it's very tedious to write that API every time you want to describe some markup. And people started hating it because they don't want to write some API to just create a markup, right?
And front-end developers were used to HTML. So they thought, Facebook team thought, why can't we come up with the approach where developer feel like they are writing a HTML, but internally we are combining it to the React elements. Why React elements? Because React elements is something which are helpful for React to do its internal work. So, so they come up with a JSX and JSX provided the ability to convert HTML or a markup or JavaScript and markup mixture to pure React elements, which are nothing but internal objects to React.
And most of people started finding it. And most of people started finding it helpful. Why? Because they're already used to it with the HTML. So they started finding it very helpful. But the problem is, JSX is something which is very proprietary to the Facebook and browser doesn't understands it. What browser understands it is JavaScript or HTML. Now JSX is something which browser don't understand. So that's why Facebook team suggested a transpiler. Now what is a transpiler? Transpiler is the program which sits between a browser and a React.
It compiles your React code in a form of code which is then understandable by the browser. So it converts all the code from a JSX to a React element. But when it converts, it converts in a browser understandable format, which is a React element. React element is just an object. An object is something which is specific to JavaScript, which then can be understandable by a browser. So you can't directly write a JSX in a browser and expect a browser to understand that. You need some transpiler. In case of React, we are using a Babel.
I mean, we will not install a Babel at any point because when we installed a create React app utility in week one, we already installed a Babel. So no need to do that step. And that is why there is that utility, create React app, which does a bunch of things for you. Without even loading you. So it already taken care. So that transpiler is just, just, just keep in mind that it is important that you feed your JSX to transpiler and then it transpiles to pure JavaScript for a browser readability.
Now, why JSX? The first thing is they thought like it will be good idea that developers are used to with HTML. So it's good idea to, you know, give some sort of HTML stylish thing to the developer. But the main point is that that React also believes in ideology where rendering logic is inherently coupled with the other UI logic. Now, why we are saying is the JSX is nothing but a mixture of JavaScript and HTML, right? Now, HTML is basically to design your view. It's design your UI. Okay. And there is something called as a UI logic. What do you mean by UI logic?
Let's say I want to display 10 employees along with their salary. Okay. So somewhere I need to hit API. Okay. And that API need to hit a backend and return the data. Now that data is not maybe in a format that you want. So you will process that data. So there is some sort of logic involved. So that logic can be termed as a UI specific logic because you are transforming a data for a UI. And then you are writing a markup, let's say table, table tag in HTML so that you can display 10 employees.
So there are two things involved over here. One, which is actual table markup and one, which is a processing required for employee data. But React believes that this UI logic and the markup should be bundled together. They are not different part. They are UI part itself. And that's why they, they came up with a concept of JSX where you can process and you can construct a markup all together. Okay. You can process and you can construct a markup all together. Now let's consider this case. Okay. Now I want to display name of the employee, his first name and last name, but I want to provide a single space in between of them.
Okay. I want to provide a single space. So Harper Perez, I want to display on the browser, but with a single space in between the first name and last name. My processing logic is what? My processing logic or UI logic is add a single space between first name and last name. And what is my markup? I need to display this name in H1. Now see this element, the cost element opening round bracket and closing round bracket bracket inside that I have written H1, I have written text, hello, comma, and I have opened the curly braces.
Whenever you need to use any valid JavaScript expression inside a JSX, you open a curly brace, write that JavaScript expression and close the curly brace. That is the syntax that JSX is following. So what I've done is I created a function where I use icon cat the first name and last name with a space. And I call that function in between a curly braces. So now this becomes a JavaScript expression and that is allowed in JSX. So when I'm rendering this particular JSX, it is calling the particular function called format name and returning me your data in a whatever UI logic I have specified in.
And then it becomes very easy, right? Your markup and UI logic bundled together. So that is the ideology of React. And that is what they come up with JSX, which is following their ideology. And then it makes the code simple and elegant too. Isn't it? Cool. So let's. Let's jump into the demo and this is a demo time. Let's go to the demo slash 4 underscore JSX and understand like how JSX looks like. Switching to the code. I hope you install all necessary requirements for this course, meaning node, then probably create a recap and then VS code and a get. So I am assuming that. So get checkout demo for JSX.
And JSX. Okay. This is my JSX demo. I'm opening in Visual Studio Code. And here you go. This is my main component. Okay. And how react application bootstrap. So we haven't touched that point. We will do that point in next week. But maybe let's just stick to the what I'm showing at the moment. Don't worry about like how this everything is working. We'll come to that part. But for now just assume that this is your root component. Anything which you're writing inside this component eventually will render on the page. Okay. So over here let me comment out this. Let me uncomment a simple JSX.
So what I'm doing is I'm calling this simple JSX component. See how I'm calling simple JSX component. While calling the component I am actually using it as a tag. Okay. Like how we write a span tag div tab. I'm writing it as a tag. It's a custom element now. See this is what react offers you. Whenever you are creating a component. Whenever you are creating a component. You no longer need to create an instance for that. You just need to write in this form. And that's it. Because that is how internally react will create a reference. And that is what they mean by when they say this part.
It's a declarative. It's a declarative. Why it's declarative. Because see we're not creating a instance anywhere. We are just declaring like how we declare our tags. Okay. So we are saying like we are calling this component simple JSX which I've written over here. We are exported that component. And over here if you see we have imported that. Now this import and export on ES6 features. You don't need to worry about that. Just understand like if you want to. Include this component in some another file. You first need to export that. And then you need to import in some another file. So that is what we are doing. We are added this component.
And that's it. Now let's understand what that component is doing. So what is this export default. Okay. So if you have a file where you need to export only single component. You can write export default. And then this file will defaultly exports only single component. Okay. If you have multiple components in a same file which we should not follow by the way. But if you come to that case any point of time then you can just export things. And while importing we just need to do something like this. And while importing we just need to do something like this. Because there are a bunch of things in that file. So you can import that by some other component. Other component maybe. What I will do is. I will copy this. Simple other JSX. Multiple components. You will export them without using default and put it over here.
And then you can actually include that also. And then you can actually include that also. And it should work just fine. So that is how you import multiple things. But now over here there is a single component. Which is why we are adding that default. Which indicates that oh this file exports only single component. And that is simple JSX. Okay. So now I am adding a render method. Why I am adding a render method. Because it must have. Okay. You should have. A render method. When you are creating a class based component. Which are extending from. Component. So. It enforce you to add a render method. And we are returning.
A markup from render method. Hello. And then format name. And then it's concat. As I indicated. It will concat. First name. Last name. And concat with the space. And it will show it off over here. Let's check it out. Let's check it out. In order to run that. I will run that. Using npm. npm start. It will open browser. And it will show. Hello. Abdelkala. Okay. So this is h1 tag. What else you can do. Okay. So let's make this. Some more interesting. You can add.
One more span. Okay. So let me add. These are here. So this markup is also got. Included inside h1. Let's check it out from h1. And then you will. See what happens. Some red things. And there is a started. Okay. So there are some issues. It's not compiling. Right. Few issues. What are the issues. JSX expression must have one parent element. Okay. That is important thing. So what the react says is. You can create a tree. But let's say. When you see a tree. The tree must have a single root element.
Right. Have you heard about a tree. Which has couple of root elements. Or five root elements. Never happens. Right. It always starts with a single root. So if I wrap it in a div. And do something like this. What happens now? Started working. Right. It started working. Yeah. So I provided a single root element at the top. If I remove that. There is a problem. But now you will ask me. That okay. I have provided div this time. But then it is adding an unnecessary wrapper.
Right. Just in order to meet the JSX requirement. I need to add unnecessary wrapper. I don't want to add a div extra wrapper. I am okay with the h1 as a wrapper. Why I should add a div every time. That is a problem. Right. Now. But. Okay. If that is a problem. Then react actually gives you. Some another syntax. If you want to just. Boil your tree up to the single root element. And you don't have a. Good reason to. Actually put a div or span.
As a wrapper element. Or you don't have. Or do you don't want to do that. Then. You can. Leave this tag. Blank. And react will understand. This is. Developer added this. Just for. For sake of. Making a single root. And it will work just fine. Okay. It just worked right. So. This is valid. JSX syntax. Right. There is no. Empty tag in HTML. Okay. This is something which. JSX is offering. Okay. But now what happening is. When actually. Okay. Let me see. Okay. So there is.
Um. Okay. So there is some warning. Which. Warning is basically. It's saying like. You are. Not using this. In term. JSX. But. You are still importing. Which is why. Our compiler is showing a warning. Now it's showing. It's compiled successfully. Which is fine. What I'm. Wanted to explain is. When you are actually. Writing your JSX. And hitting a save. Your webpack is getting. Recompiled. I mean. When you. Do this save. Now it's happening so quickly. As that you are not able to view this. But when you hit a save.
There is a compilation process. This which is happening in the background. And in that compilation. This JSX is getting fade. To babble trans filer. That babble trans filer. Trans files. This JSX. Syntax. To a. React element objects. Which can be then understandable. By the browser. So far so good. Okay. To see. Let's go over. Interim JS. Interim JSX. Let's see. What is that. Toward the simple JSX. What is. Interim JSX component. Let's look at that. Okay. So I toggle this. I just switched it off. I switched it off.
And switched this on. Now let's look at this. What I'm doing over here. Just one step further. So let's say. I want to greet. I want to greet a user. Okay. So. I want to greet a user. And the data is this. The first name is Abdul. Last name is Kalam. Okay. And title is Sir APJ Kalam. Okay. This is my data. And. If. If I'm passing a user. To a function. Let's say. Over here. I'm passing a user to function. Right. Then it should greet that particular user.
But if there is no user passed. To that particular function. Get greetings. Then it should show me. Hello stranger message. Okay. So there is a conditional way now. There is a conditional thing in between. Well, yes, you can write a JSX in any React component, but you need to have a React in CONSEX. I mean, this is also one of the components. This is a functional component. This is a class-based component. And what you're doing is you're including a React. Okay. So this is also a component. This is also a component.
But you can call the component directly also. Okay. So now we are calling the component directly over here and we are passing a user. Now inside this component, we are just putting a check. If user, if it is a user, I mean, if user exists, then return this markup. Otherwise, return this default markup. That hello stranger. So you understand? I mean, we are conditionally returning the JSX. It is also possible to return a JSX conditionally. Okay. So inside H1, I am calling a function and that function or a component actually produces the two different subtrees.
This is a one sub-tree and this is another sub-tree. And this H1 differs from this H1, right? They are totally different, right? But still it works because at a time only one condition holds two. So if I run this, now it will show a correct rating. Hello, Abdul Kala. But now if I don't pass the user, it says hello stranger. So you can write JSX in conditions and that is perfectly fine. One more thing. You can specify JSX not only inside the tags, but inside attributes. Let's say your H1 tag has a title attribute.
Now why we use a title? To show the tooltip, right? If you want to show a tooltip on any HTML element, we use a title attribute. And what we are doing is from this data, we are picking this title and assigning to the title property. So here, what we are doing over here is we are basically getting a greetings based on the conditions, which I already explained. And what I wanted to explain is now and just repeating the part of the attributes, which is this part. And we'll start explaining this again, how you can pass data inside HTML tag, you can pass a data to attributes and it's fine in JSX.
For example, you have a title attribute and inside a title attribute, you are passing user.title. So from this data, you are basically picking a value of the value of this title property and showing over here. It's possible to do that. Like how you use a JavaScript expression over here. You are also using expression over here, which means you are interpolating this variable and assigning to the title attribute. So if you do something like this, let's do that. Let's pass user over here. Let's check. Let's hover over here. Can you see any title? Yeah.
If you hover the H1 tag, you see the APJ column. So title is appearing. So JSX is not only for tags, but it's for attributes also. So three things. You can use JSX conditionally. You can use JavaScript and HTML in combined in the JSX and third, you can use JSX for specifying values to your attributes. You can use JavaScript. You can use JavaScript expression for attributes also. So that is a basic thing you will need when to work with the JSX. There are a few advanced things which we will cover when we come across those concepts.
Okay. Let's move on. Okay. Can browser detect JSX? No. So browser can only work with the HTML CSS JS. JSX represents object. So internally it represents object. Right. So we are using a Babel compiler under the hood of the webpack to basically convert a JSX to react element. So if you see how that compiler works, it's something which we don't need to know. What we just need to know is this is what your JSX is. This is what your JSX is. And once your Babel transpires it, it looks something like this.
Now this is something the left side part is something which browser will not able to understand. But right side is something your normal JavaScript object and you are calling react.create element. Now this react reference is coming from the inclusion or importing your importing react, right? It's a same react reference and you are using .create element and then doing bunch of things over here. It's another way of saying the same thing. Okay. Okay. But this is very useful, right? I mean, this is something very familiar for HTML developer. And this is something like imagine you are writing this every time and that will be very problematic, right? Over the period of time. So that is why JSX is very handy and we are writing everything in JSX.