Podcast Lookup

React JS Conditional Rendering Explained – Master Dynamic UI Rendering in React

ReactJS Developers Podcast

  • December 3, 2026
  • 1 h 1 min
  • Episode 2
Episode page
About this episodeIn this episode, we explore Conditional Rendering in React.js, an essential concept that allows developers to display different user…
In this episode, we explore Conditional Rendering in React.js, an essential concept that allows developers to display different user interface elements based on application conditions, user interactions, and data states.Conditional rendering makes React applications dynamic and interactive. Instead of displaying the same content all the time, developers can control what users see depending on factors such as authentication status, available data, user permissions, and application state.Through practical examples, you'll learn how to implement conditional rendering techniques and create more responsive React applications.Whether you're a beginner learning React or a frontend developer looking to improve your skills, this episode will help you understand how to build dynamic user interfaces efficiently.What You'll Learn
  • What conditional rendering means in React.

  • Why conditional rendering is important.

  • Using JavaScript if and else statements in React.

  • Implementing conditional rendering with ternary operators.

  • Using logical AND (&&) for conditional UI elements.

  • Rendering components based on application state.

  • Handling loading, success, and error states.

  • Conditionally displaying components based on user authentication.

  • Practical conditional rendering examples.

  • Best practices for writing clean React components.

Who Should Watch?
  • Beginners learning React.js.

  • Frontend developers.

  • JavaScript programmers.

  • Computer science and software engineering students.

  • Developers building interactive web applications.

Transcript

From the publisher

This transcript comes from the podcast’s own feed, shown as the publisher wrote it.

React JS Conditional Rendering Explained – Master Dynamic UI Rendering in React

00:00—
  1. Speaker 1

    Hello, today we are going to do something like this and something like this. Now you must be thinking that what is the big deal in doing this? Conditional rendering is a little basic, you told us to use it and we will do it. So definitely today we are going to do conditional rendering. We are going to study about it. Now because we are going to study, we will definitely get to know a lot of things. Conditional rendering is there, but not only as much as you are thinking. You are thinking that by applying ternary operator, our work will be done. But this is not enough in conditional rendering. There are a lot of things and I will tell you all of them now. So basically I have told you the use case that whenever you want to do conditional rendering, the meaning of conditional rendering is something like this. For example, I clicked on show content, so here you can see the content. I clicked on hide content, it has been hidden. Now this was basic. If you want to see more practical use case then you can logout and you can see different things on logout. Account details are removed. And as soon as you log in, you get your details and you get additional functionalities on log in. So you can do all these things through conditional rendering. Although database is also involved in log in but we will see basic without database. So, as soon as we are doing show content, the content is being shown. As soon as we are doing hide content, the content is being hidden. So, first of all, we will see these small things, how we do it. We will go in depth as always. So, let's get into the notes and then we'll go to the code. So, conditional rendering. Today's topic is conditional rendering. That means, on the basis of any condition, run a component. On the basis of any condition, run a component. If the condition is A, then you have to run a different component. If the condition is B, then you have to run a different component. And if there is another condition, then you don't have to run a component. Okay. Something like that. Rest you will understand well. So, let's move ahead. React does not have if else statement inside JSX. Now, listen to me carefully. Okay. Because we will use if else also. You will say further. You said you don't want to use if else. React does not have if else statement inside JSX. So, read the statement carefully. React does not have if else statement inside JSX. Okay. Inside JSX. JSX means the curly braces we use. This is my paint, right? It's dead. Alright. So, let's say I have written H1 here. Let's remove H1. I have made a div here. And our div is closing here. We have to write JavaScript in it. So, we can definitely write JavaScript in this way. So, this is our whole JSX. We are writing JavaScript in it. Alright. So, we write JavaScript in it by putting curly braces. Here, I had told you that you have to use expressions here. You can't use statements. If else is a statement, then you can't use it here. So if you can't use if else statement, then how will we render anything conditionally? Conditionally means on the basis of condition, if this condition is true, then you have to do this. Otherwise, you have to do this. So how will you do all these things? So I will tell you everything, but here you understand a little carefully that inside JSX, inside JSX, we can't use if else statement. Inside JSX, Inside. We can do it outside. Alright. Instead, we use conditional rendering to decide what to show. Right? What do we do? We do conditional rendering. There are many ways to do conditional rendering. We will see all the ways. So, we can decide from conditional rendering that at what time we have to show what. Okay? At what condition we have to show what specific things. Conditional rendering means displaying UI elements based on certain conditions or states. Oh man! I don't know what happens in the pen tab. So now I will be looking very different because I am recording this video in the evening. In the morning, our pen tab got spoiled and the whole mood got spoiled. I tried a lot to fix it but some things are working and some things are not working properly. I am not able to write properly. So, I can't do anything in that. Let me see. I will have to buy a new one. So, for now, I can't write. So, I will underline it and explain it to you. So, you will face some difficulty in understanding. And it will be difficult for me to explain. Because without writing, it seems a little deep. Anyway, no problem. We will not back off. We will understand and go. Okay? That is my responsibility. How will I explain? Look. So I was telling you that conditional rendering means displaying UI elements based on certain conditions or states. Do you know the idea of states? Do you know about conditions? Some conditions are used, greater than, less than, equal to, not equal to. So check whether the condition is correct or not. There will definitely be some value in the state, whether it is a true value or a false value. So on the basis of truth and false, if else you have done it in normal JavaScript. So on the basis of truth and false value, this condition is definitely checked. So we will decide on the basis of conditions and states that which component will run and which component will not. So that's it. I am with you. And what was this language? Anyway, let's move forward. So this much you will have a little idea of the false statement of the conditions of the ternary operator. And if not, then you will know it now. There is no problem with that. So first of all, we will see how we can do conditional rendering with the help of if-else. Now you will say that I had told you that you have not forgotten, you are coming in front, so you have not forgotten that we will not be able to write if-else statement here. We cannot use if-else statement here. But I had also said that you cannot insert if-else statement inside JSX. Let me tell you how to insert JSX. Here we are writing JSX and here we are inserting Javascript. Here we have to insert only expressions and not statements. This is what I said. I didn't say that you can't use the first statement. You can't use it in JSX. You can use it outside. So, definitely I will show you how to do conditional rendering. And I just gave you an example. I showed you an output. There was something like message is coming, message is hiding, something like that. So, you must know that. You must have seen just now. So, keep that in mind. We are going to discuss all those things in this. Now I will remove it from here. And this is an application of our VIT. You must have known this much by now. We have discussed a lot of lectures. And we have made a lot of projects. So definitely you will have an idea that I am using VIT. And if you want, you can use Tailwind CSS. So you can install this as you want. You have an idea. Alright. So now I will zoom it a little. I will close it. So that If you are watching it on your mobile phone, then you can see it clearly. So, our project is also running. Now, we have to do conditional rendering. Conditional rendering means we have to render something on the basis of a certain condition. And if the condition changes, then we have to render something different. So, how will we do it? Let's see. Here we are doing return, what are we doing return? We are doing app return. Here app is basically a div tag in which app is doing return. So here we can see that. If I want to return something else here, like I want to return h1 here. Hello, school for you. So here we can see this. Hello, school for you. Alright. So now I want to show something else when the user log in. And if he doesn't log in, then I want to show something else. So how will I show this? Assume that user is logged in, so I want to show here Welcome back, welcome back, you are logged in. Okay? Logged in, okay? You can see it. You are logged in, alright. Welcome back, you are logged in. So I want to show this when user is logged in. So I will make a variable here. Const is If login is true then only we have to show this. So definitely I will put this statement in if condition. So if login is true then only we have to run this. If login is true then only you know that it will run if. You have done a lot of truthy and falsy values. I am assuming. And if you haven't done it, then understand it this way that if there is any truth value here or if there is any value here, then it will be considered true. And here you will have this return. Okay. I will give you an idea of the truth. If you are writing some string, then this is your true consider. If you are writing true, then definitely this is true consider. And if you are writing some number, then this is also your true value. So definitely if you have all these things, then this statement will work. That means this is the truth value. Something like that. Go and see it properly. So if you have login here, then this one will work and will return. And you can see it here, but if it is not login, then if it is false, then you will not see anything here because you have not told that if it is not login, then what to do, if it is login, then it will work, it will return from here, but if it is not login, then I will give one more return statement here and here I will show Please login. If you are not logged in, then please login. If you are logged in, then this block of code will be run and it will be returned from here. This output will be seen. But if it is not, then this will not be run, it will be forward and this will be returned. So you will be coming here, please login. And if you are logged in, then you will be coming here, welcome back, you are logged in. Now I can apply multiple conditions here. If you have done JavaScript, then it is very easy for you. Let's take another scenario here. I give the option A, B, C. If the option is A, then I want to check the whole condition. If the option is equal to A, then I want to show component A here. If option is equal to 1, then I want to show the component and let it be and copy paste it. If option is equal to b, then I want to show component b and copy paste it. If C is equal, then I want to show component C here. Otherwise, I want to show some other component here. We have not made a component yet, we are just playing like this. If option is A, then this code will be returned. If option is B, then this code will be returned. And if option is C, then this code will be returned. And if there is nothing above, then this code will be returned. Let's see what is being returned here. Component A. Why? Because we have given A in the option. If we do B in the option, then it will match with B and B will be returned. Did you understand? If it was A, then this code will be returned. And if this return is not there then it will definitely move forward. It will go to this and see if B is matching. If B is matching then this return will be there. It will not move forward. Alright. And if I do C here. If I do C here, then you will understand that component C will come. And if I give something random, D, E, F, something else, then other component will come here. Okay. This was very easy. This was very easy. It was very easy. So... What to do with this? You have given the code here. See, the code is given, so you don't have to worry about the code. Should I remove it or put it in a different component? Let me put it in the component. Okay. Here I give the if component. If.jsx and here paste. Okay. So you will also get these notes. Has it gone out? Let me put it in src. Okay. I will make a folder here and put it in notes. Now we have to see how to do conditional rendering. Don't try to use if inside JSX tags. Don't try to use false statement in the curly braces syntax because you can use expression there and not statement. So keep this in mind. React component can return different JSX depending on logic. It's an obvious thing. We just did it. So using ternary operator. This is used a lot. You will use it a lot. Here you will be doing conditional rendering using ternary operator maximum times. Okay. How to do it? See that. Here syntax is very basic. You must have read in java script too. That here are two things. First of all, here is a question mark. And here is a colon. And there are three parts. The first part is our condition. The second part is our component A. And the third part is our component B. Component is given here, in ternary operator there are three parts, one condition is given, if it is true then this part will be executed and if it is false then this part will be executed, something like this happens in normal javascript. Here we have to render the component, so we will put the component here and play. You can do anything else. It depends on your will. So, here condition. If you have true, then this first block of code will be executed. Listen to me carefully. If condition is true, then the first block of code will be executed. And if condition is false, then the second block of code will be executed. Just keep this in mind. If it is true, then the first one. If it is false, then the last one. Okay? Come here. So, let's see how to do it. Again, I make the same variable here, const isLogin is equal to true. Now, if I want to use JSX here and I want to insert JavaScript in it, then I will use curly braces in this way. And here I can write like this, isLogin question mark, there are two parts, the first part and the second part. First of all, the condition will come here, then the first one which is visible here and the second one which is visible here, we can put our component here. If your condition is true, then here I make it like this that logged in and here I make it like this, please login. So, do you understand what I am saying? I will format it a little better. Let's close it. So, look carefully, there are three parts here. 2nd and 3rd. First one is condition. If condition is true, then first one will execute. If condition is false, then second one will execute. If condition is true, then second one will execute. If condition is false, then second one will execute. That's it. Very simple. We are using maximum times. Maximum times. Okay, that's it. Just keep in mind that if it is true, then the first one is false, then the second one will be executed. Alright. And some notes for you. Avoid complex ternary with multiple question mark, question mark, question mark. So, you don't have to do multiple nesting. It can be a little bit nesting but you don't have to do more nesting otherwise the code will look confusing. The readability of the code will be damaged. For example, I will give the same option example. const option is equal to, I will give it here for now. keep in mind this code was looking so beautiful to see that if it is this then it will be execute this then it will be this then it will be this then it will be this now I will show you by doing it with ternary operator if your option is a option if your option is a then here you have component component a execute will be done definitely if it is true then first one you have execute will come component a and if it is false then you have Other components will come. No problem should be there. So this was a simple ternary operator. We have not used one ternary operator yet. We have not done nesting. So it is looking very simple. It is not looking confusing. So component A is coming. If I give something else here. Wait a minute. If I give B here. Then you will get other component. But I don't want to show other component. I want to do some more nesting. So I will put some more condition in this. So what I will do here is to remove this and look carefully. You have a full ternary operator. We have written the whole thing using the ternary operator. So if it is A, then you have this executing. And if it is not, then I want to execute B here. Component B, not this one. So what will I do here? I will remove this here and do it again in this way. Any problem? Did not understand, look carefully. That's why I was saying that it looks a little confusing, so use it with understanding. Code's readability gets damaged. Here option is A, so this is executed. But if option is B, then I want to execute B component and if there is any other option, then I want to execute other component. So here you can use Alternate Operator and check if option is B, then you can execute B or other component. So what I did here is, paste it again. If it is B, see if it is not A, then only it will come here in this portion. So here again it will check that if it is B, then here you render component B. And if it is not B, then here you will get other components. Okay. Let's format. So this is how your code is visible. Now here component B is visible to you. And if you put C, then definitely you will see other component. And if you put A, then the first one will be executed. So component A will be visible. I told you that there are 3 parts of a ternary operator. The first part is the condition. If it is true, then the first part is executed. If it is false, then the second part is executed. If it is true, then it will be executed. If it is false, then it will be executed. If it is true, then it will be executed. If it is false, then it will be executed. So I have to apply more conditions. So I used another ternary operator and if option is b then show component b and if not then show other component. I can apply more conditions like this. I can remove this ternary operator and apply more conditions like this. If you have option C, then component C will be rendered otherwise other component will be rendered. Now you can see something like this. Again the same thing. Let's keep it like this so that the code looks a little readable. Although it will not look readable. If you do more nesting, then it will not look readable at all. See how confusing it looks. If option A is here, then this will be done, otherwise this will be complete. Now if this is complete, then your condition will also be checked in this. If option B is here, then return option B, otherwise this will be complete. Then it is also being checked in this that if option C is here, then return option A, otherwise this will be complete. It seems a little confusing. So, I have given in the notes that avoid complex ternary with multiple question marks. Basically, you don't have to use nested ternary operator. You don't have to do nesting. If you do nesting, it will seem a little confusing. It is better to use the if-else statement. If there are multiple conditions. Otherwise, ternary operator is best. If there is one or two conditions, then ternary operator is best. If there are multiple conditions, then if-else statement is best. Okay? Code readability is very important. So, you understood ternary operator. Alright, now let's move ahead. So, I will give you some notes. Here, I have given you this too. ternary.jsx But I don't understand why you need code. Why can't you do it yourself? I didn't ask this in GitHub. I was getting messages that give code in GitHub. But what will you do with this code? If you make it yourself, it will be more fun. What will you do with this code? Right? So now we have to understand the use of the end operator. I have already told you all this in javascript. I had taught you short circuiting. If you have studied, then I had told you that short circuiting is going to be used a lot in react. Conditional rendering is going to be done. So I thought a lot and designed the whole structure there. So that your javascript is covered at a very advanced level. And then you can easily do react or backend. I told you that after designing it very well. And in the same way, I am designing react as well. If you get it properly, you can also go to next.js. You will handle the frontend completely. So here, rafc. Alright. And now we have to do something like this. Till now, we wanted something like this. If the condition is true, then execute it. And if it is not, then execute something else. Such a condition can also come. Such a Case can come to you that if that condition is true, then only we have to execute and if not, then we have to do nothing. If not, then we have to do nothing. For example, for example, here I have taken a beautiful example for you, notification one, that if you have message, then only you have to show message. If message is not there, then what will you show? If message is not there, then what will you show? Okay? So here, here, if you want to put condition like this, assume I make a variable here, const has message is equal to I will take some message here Hi Muskaan, how are you? ok and here I will definitely use JSX and here I will use ternary operator hasMessages if I have messages so now you know how to do something here wait a minute If there is a message, then something, something, something will happen here. If not, then something, something, something will happen here. Okay. So, if there is a message, then definitely I want to show a message. Message will be returned. And if there is no message, then what will I do here? Tell me, then what will I do here? What will I do? What will you show here if not? Okay. So, now you are stuck here. So, for now, you can show null. This is an option you have. Okay. And why did I put this? So, now come here and see carefully what you are seeing. You are getting Hi, Muskaan, how are you? Wait a minute. Wrap it in div. Sorry, wrap it in h1. So it will look a little bigger. Something like this. And here, if we want to use it as a variable, then something like this. And let's decorate it a little. Something like this. So now you are understanding it well. If there is a message, then I want to show this message in h1. And if there is no message, then I don't want to do anything. I want to show null. See, how is it happening? Because we have a message now. But if we don't have a message, If we don't have a message, then nothing will be visible here. So one way is that you can do it this way, you can do it this way, you can give it this way, if it is there, then show it this way, otherwise null it. But you have better options than this. You have a better option than this with short circuiting. If you have not done short circuiting, then definitely go to JavaScript and do it. Because our pen tab is also damaged, so I will not be able to explain to you. I will give you a little idea, I will tell you in a very simple way, you will understand. If you want to learn in depth, then definitely watch the short circuiting video. So here, I comment this, not like this, we will do something like this. Something like this. If you have a message here, then only, then only, we will use the end operator, then only, here you run this, otherwise not run. As message. Okay. If there is a message here, put this. If there is a message here, then only your component will run. Okay. Here short circuiting is basically what happens. If paint ever happens, I will definitely tell you again that short circuiting is a little bit. If it is not there, then you will not understand. Here it happens that you know that both the end operators are true, then your true is being evaluated. But it is not exactly that it is being evaluated in true. Here you get the output as it is. Okay, how do I tell you? Look, understand it this way that your code goes from here to here. If it was false, So, the game was definitely over here. Right? What happens in the AND operator? All of them should be true. All of them should be true. So, if you got false in the starting itself, then definitely this will be your return and it will not go further. The circuit will break here. But if it is true, which it is definitely, it has some message, I will put it here. Hi, it has some message, that means it is a truthy value, then it will move forward. It will move forward and see if there is something here, if there is a truthy value here or not. If there is a false value, then definitely nothing will be shown here, nothing will be returned. Okay, the false value will be returned. But if there is a truthy value here, then this will be returned and if there is nothing in the future, then it will go ahead and check. Something like this. And if you understand it in a simple way, I told you in a proper way, you will definitely not be clear here. If nothing is coming, then So, here you simply understand that if there is a message, then only it will be returned to you. If not, then it will not happen. If there is a message, then you can see a high here. If there is no message, then it will not work for you. Just keep it like this. And if you want to see what is short circuiting in detail, then you can definitely see. Okay, here the people who sit in a row, they work like this, they work. But if I want to show it to you further, then I can also put another end operator here. See this. I am writing here h1 no message. So now see what you have here. Let's reload. Wait a minute. We don't have the message here. So see we don't have the message. It is false here. The message is not there. It is a false value. So if it is false here, then this will be the full return. So let's put something here. Like if I had put zero here, then you can see zero is coming here. The fallsy value is returned. It doesn't move forward. The circuit breaks there. But if there is a truthy value, like there is a message here, high. So if the message is high here, then definitely it is true. So it will move forward. It will check here. Is there any value here? Definitely it is here. So it will move forward and check here. Is there any value here? Definitely it is here. It will not be able to move forward. So this will be returned. This will not be returned. This will be returned. No messages. See. A little bit. You can see this separately. Those who have kept the react as a rut, that this is how it works, here we put the end operator, if both are true, then your output is visible here, if one of the two, means basically if there is not much, then your output will not be visible here. These are rutty things. These are actual things that you are understanding here. Okay. So if there is a message, then definitely your last one will be visible if you keep putting the end operator. If there is a false value in the middle, then it will be returned. If there is a message, then it will be returned. If there is no message, then nothing will be coming. If message is there then it will be returned and if message is not there then nothing will be coming. There is no alternate option here. If message is there then it will be returned and if message is not there then nothing will be coming. I have told you about short circuiting in depth. I have explained the concept of short circuiting in depth. You will be clear about all these things there. The other simple way to understand is that if there is a message, then only you will be able to see whatever component you have rendered. And if there is no message, then you will not be able to see this either. Okay, that's it. So let's move ahead and now we have that we How can we render using the variables? You will find this file similar but here it means to keep JSX clean. This approach means to keep JSX clean. Don't make JSX dirty. Do whatever logic you want to do. I will show you what it means. Do we need this code too? Take this too. What was this? this was something dot jsx let's see here this was our and operator ok and dot jsx remove this rafc and format document ok so here I had to use variable so what we will do here we will do just this much that in this way we will put content here So, we will put our JSX in the variable content. Hello, how are you? We have already created the component that we wanted to show here. No problem, the output will look the same as it used to look. Let's come here and reload. Nothing is visible, we have not returned properly. Everything is fine here. Content and content is also coming here. The problem is that we have not given const here. Now it should be visible here. Hello, how are you? Now it is coming properly here. Now the meaning of using variable here is that keep it as it is here. You do not have to spread any rules here. Whatever you have to do, you do it above. Write all the logic above. means, till now I was telling you this way, now I will tell you by making a component, you will understand better. So I make a component folder here, component, c1p1nt component, and inside the component I make 2-3 components, component a, because we have to use it again and again, obviously, component a.jsx, then component b.jsx, new file, component b.jsx, Let's make it one more time component C dot JSX all right rafc and rafc here also rafc all right so these three components have been made now what I will do here definitely I will import all three components and here let's import directly component A why are suggestions not coming? Suggestions should have come give us suggestions Component A is not getting the suggestion, why? Component A, everything is fine here. Suggestions should have been received. You can also write by yourself, there is no issue, but if you get suggestions, then our work is done quickly, right? Let's close this. Component. Give me the suggestion, don't do it like this. It's not giving, it's okay, let's import here. Import. component A from dot slash component slash component A. Okay. Let's replicate this. We are also very smart. A, B and C. Okay. Here also A, B component and this is our C component, so now we have to show here, whatever we have to show here, now we will save it in variable, otherwise it will be visible to us, why? because we are using variable here definitely, we are using variable here, so whatever we will put in this variable, it will be visible to us here, so condition we will put in this way, suppose here we have option as always, A, B, C, something, something, something, now here we will put if else condition, means logic we have kept separate, and GSX we have kept separate, okay, so logic work will be done here, and option will be is equal to a and option is equal to a then we will put component a in the content ok like this else if option is equal to b then we will put component b in the content ok else we will put component c in the content one minute component c And here spelling is wrong. Let's do option. Alright. So what we were doing earlier, we are still doing the same thing. Earlier we were returning the whole thing again and again. Now we have made a variable here. We have given our JSX inside the variable. So now we have to change the JSX from the variable. Now you can put JSX or put the whole component. It is your choice. So if I want somewhere, like else if our option is equal to C. So here we will show component C. Else we will put H1 in the content. You can also do it this way. So now the logic is here and the JSX is separated. We just had to do this much and now it will be running perfectly. And I believe you will understand that these are very basic things but the method of doing it is a little different. So, what we have to do here? We have option A here. So, our option A will match here and component A will come in the content. And what will be visible in the content here? Definitely component A. So, let's come here and reload. Control shift I. There is some problem here. Let's see what the problem is. Assignment to constant variable. Alright, we have tried to change it to a constant variable, which we can't do, definitely. So, I have made it let. Alright, let's see what is coming here. Look carefully, component A is coming. If I make option B here, then you can see component B is coming. If I make option C, then definitely this C block of code will be executed. And component C will come in the content. And here content is being rendered. So, here C is coming. If I put something else, R, F, G, anything random, then we should have other component here. We have added other components in the else block. You can do it this way also. If you want JSX and logic to be clean then you can do it this way. When we make projects, I will be using all these things. You will think that I have done nothing separately. Everything is the same. If you know how to use a fill statement, you know how to use a ternary operator. The rest of the things are just to separate the method. It is the same. Next we have conditional CSS and class name. You can do this one by yourself. You can do the same thing in class name. Just like we were doing here. You will have to give the code. So here in notes, variable.jsx and here I put rafc and here I put h1. Hello, how are you? And here I use the class name. BG red 500. So, let's see what is coming here. BG red is coming. Alright. Now, I want to render the class name or CSS conditionally. How will I do it? I can do it here. I will make a variable here. I will give the name of the variable. Color. Our color is red. Alright. So, I want that if red color is there, then only red should be visible. And if red is not there, then our red should not be visible. So, I can render it conditionally here. I will do it like this. I will use the template literal and here I can insert the variable definitely. You know this syntax, I am using the normal template literal and I can insert the variable definitely. If it is red, then I will do something else. If it is red, then I will give bg red 500. Otherwise, I will give bg blue 600. Now we have an error because we have made a mistake in the syntax. We have added a comma here, which we did not have to add. So now we are not getting anything here. Why is it not coming? If our color is red, then bg red 500 had to come, otherwise it had to come. And here we have also left the const. So how can we make such small mistakes? If I give something else, it will be blue. You can definitely go to nested, but I will not recommend you to go to nested. You can do all the logic work here and try to keep JSX clean. Our color is red, otherwise it is coming. You can give something else like opacity 50. If it is red, it will be like this. and you can also give something like opacity 50 here so if it will be red then something like this so in this way you can also do conditional rendering in CSS it is not necessary that you have to do conditional rendering in JSX or components you can also do it in CSS you can make a whole CSS class and do conditional rendering here light or dark you can do light dark mode work here and you can also use class name if you are using Tailwind CSS so this It depends on you. Okay. Conditional rendering with state. Now it will be fun. Because now we will be able to work dynamically. You must be seeing it little by little. I told you in the starting. This is happening when you click the button. And then it is happening when you click the button again. To do all that. To do all that. Definitely we will need states. See how. I will show you now. State is the only thing that we change, so it reflects in the whole UI. Do you agree? So definitely we can use state here. I will make here toggle comma set toggle is equal to use state and by default I will keep false here. Now what will I do here? Now I definitely have to write JSX here, so I will close curly braces open like this. I will write here If our toggle is true, then do something and if it is not true, then do something. Suppose if our toggle is true, then I want to show here div, not div, let's show in h1, it will look a little big. My message is this. And if toggle is not true, then I will show here in h1. Click on toggle. Toggle will be true if you click on toggle and if you click on toggle again then you will see this. So basically I am rendering conditionally. Now if it is false then click on toggle and if I make it true then I will say that toggle is true. Toggle is true. You can do as you want. I am doing this to make you understand. So now we will do this work with the help of a button, we will definitely click on the button and if the toggle is true then we will make it false and if it is false then we will make it true. We can do this work easily, we have just studied event handling, so let's make a button here and in the button Let's write toggle and onclick here. Let's make a function here too. Set toggle to not of toggle. If it is true, then it will be false. But when I click again, it remains false. So what I need here is if it is true then do false and if it is false then do true. So I will do not of toggle here. So if it is true then it will do false and if it is false then it will do true. So now when I will click on toggle then toggle is true. I will close it. Toggle is closed. Click on toggle. It is telling here. Okay. Instead of click here we will do this. that toggle is false now you can understand it well by default we keep it false so here toggle is false toggle is true so this way you can render conditionally with the help of state When you change something in the state, it reflects in the whole UI, it is re-rendered, we have read all these things, so as soon as we change in the state, our toggle changes, our toggle changes, so conditionally all these things are also changing. So it is very easy, again I am saying 2-3 things, short circuiting, logical operators, if you are clear, and if you have normal ternary operators, then there is nothing different in conditional rendering. And I have said in almost every lecture that this is a complete java script and nothing else. So here we come to returning null. So we have seen the null one that if you want to hide something completely, then you can return null. For example, let's assume that our toggle is false, then we don't have to show anything here. So I will put a condition above here that if we have a not of toggle, then we will return null here. So, there will be no return and it will not move forward. If there is no toggle which is false, then we will not see anything here. So, in the starting, you can apply the condition like this. So, many times you will see the condition applied like this. Mostly, we do this in pop-up. If our pop-up is false, then in the starting, we do null return so that no component is coming there. If toggle is not there then return null means you can do all these things definitely. Returning null means render nothing. Common in model and popup components. If you are making popup and model then we use all these things. And definitely I will make and show you. Not in this lecture, but in projects. And now I will tell you some more advanced things. Like you can select components conditionally. Many people don't know that we can do this here. So I will tell you how you can do it. It will look like a variable, like we were putting the component in the variable. It will look like that, but the method of using it is a little different here. Here we can work in this way. First of all, I have to give you notes. So I will create a file here state.jsx and this is our rafc. So now I have created a lot of components definitely. So I will try to use those components again here. Component a not like this from dot slash component slash component a. And let's replicate this here. This is our component b and this is our component c. Component B and Component C. So, you know this much so far. You know this much so far. If I want to use Component A in this way, I can definitely do it. If I want to use component B, I can also use component B. And I can also render conditionally here. But how can we select the component conditionally? We have to learn this. If I make a component variable here, it is a little variable, but here we are assigning a component directly. We are assigning this component and using it as a component. There is a slight difference here, we are not using the syntax of curly braces. Here we are selecting the component in such a way that component A is inside this component and we are using the component here. We are not putting the whole component here in this way. You will get an error here, if I do control shift I. If you are putting the whole component, then you have to do the component in this way and you can see the component here. But I am not putting the component in the whole variable, I am selecting the component in this way. component A. So this variable is component A. And here I can use it like a normal component. I have just put the name of component A here. So this variable is considering component A and as we use this component, component A is coming here. Now here I can render it conditionally like if I make an option here or make a toggle, let's make the option of toggle, const option is equal to A and here if the option is equal to A, then we will select component A here, otherwise we will select component B Something like this. And here will be our question mark. So if our option is equal to 1, then our component A will be selected here. That means this component is component A. Okay, it is referring to component A. And if our option is something B, then our component B will be selected. That means if it is false, then it will not work. So component B has been selected. And this component variable is referring to component B. And here we are using it like a normal component. Okay, like a normal component. So, you can work in this way. So, component B is also coming. And here let A be, do B here. If I put B, then B is coming here. If I put A, then A is coming here. You can do something like this too. It's a little syntax, just a little bit different syntax wise. There is nothing different. We are working in the same way. So you understood this too. Now let's move ahead and see what other things are there. Using switch statement. Now you will do this yourself. When you have three or more conditions, you can use the switch case statement. I have told you the if-else statement, so is there anything difficult in the switch case statement? No, it is not difficult at all. You can use the switch case statement here. You will pass an option here, you will make it case wise, what case you have to run here, if the switch comes, then it is the same thing, there is nothing different, you just have to return here, you have to return JSX or component, it is your choice, you can do whatever you want to return, I'll show you by doing this too. Come here. What was this? This was our select component. Here select.jsx and I put it here. RFC. Okay. See, remove the return here and use the switch here. Like this. Switch is also simple. You must have read the switch before. I will make an option here. Now I will make cases here. If the case is matching with A, then I will return something. If it is matching with B, then I will return something. If it is matching with C, then I will return something. It is easier to see if you have multiple conditions. So let's take case A, if I want to return component A, I will import component A from dot slash component slash component A, alright. And here component B, okay. B is here and component C is here, okay. so if our case is A then I will return component A component A will return like this actually like this and if our case is B then I will return component B and if our case matches with C then I will return component C and in default case, I will put it here. Don't miss the default. In default, if you don't want to do anything, then give null. Otherwise, if you want to show something else, like I want to show other component here, then I make another component here. If it matches with case A, then A will be seen. If it matches with case B, then B will be seen. If it matches with case C, then C will be returned. And if it doesn't match with anyone, then other component will be returned. That's it. And I don't think that you need to explain the switch case statement. I don't think that you need to explain the switch case statement. So here component A is coming. And if I do B, then B is coming. If I do C, then C is returning. And if I do D, then other components are coming. So, you can see that the switch case works like this. So, it is very easy. So, there is no other way to make the code readable. Okay. Using mapping. Now, you can also work with this mapping. For this, you should have a little idea of the object. Like, you have made an object here. Wait a minute, I will give it again in notes. What was this? It was switch, right? Switch.jsx. and here rafce and here also I have to make an object alright so I make a const status object and here I will be rendering component on the basis of key. We use normal object in that way. And I am saying this as mapping definitely. We are doing our work by mapping. Like if our status is loading, then I want to show this way in H1. Loading. Okay, here you can pass your component as always. As I have shown you. Pass component or give something like this. So loading. Error. Okay, I will give error here. Error. Come. and then I will show you something else success H1 successful all right so here I have basically made three components and all three are saved in this object loading error and success so now you know how to access it like I show you by doing CLG I did CLG status And if I want to see the status of the ship, then I will see an entire object here. If I want an error from it, then I can access the error from the dot notation here. So here I will see the error one. Now you have a whole JSX, HTML basically, so it is coming in this way. If I want a success one here, then I can definitely see the success one. So here I have a successful one, if you do it a little bit like this, then you can see in children that it is successful, which you have here. There is one more way, we will definitely not be able to use this way because we have to work on the basis of variables. Here I give the name of a variable and in this status option we have loading, error, success, anything can come, so on the basis of this I will render it here. How? See. If we want to insert a variable, we can insert the status option variable in this way. If there is a loading in the variable, then the status of loading will be visible, that means it will be visible to us. If we have an error here, then the error component will be visible to us. You have already done the normal object. Let's reload. See, you have the error one. So in this way, we will render this whole thing here. In this way. Here. we will return this no problem should happen so if we have error then error will be visible here if we have loading like I will load in option now you can make same thing in state also here you can make state also like I will make state only and show you it will be fun so status status option option comma set status option is equal to use state and here false not false by default I want to keep loading ok so here we can see loading alright now I want to change it on button click let's say I want to change it on button click so here I have to use JSX to show it alright alright alright alright And I put a button here. I write loading in the button. And then I make another button. I write success in the button. Then I make another button. I am doing all these things to make you understand. In the real world scenario, you will do something like this. But you will do something different. Success and error. Error. Alright. So here I put onclick. Onclick. And here set option. In status option, I put I will set the loading and in the same way we have more buttons, so I will put on click in more buttons in this way, on click is applied in this, on click is also applied in this, If I click on the loading button, then I will put the loading in the status option, then the loading component will be visible here. And if I click on success, then the success component will be visible. And if I click on error, then the error will come here in the status option. So according to the error, the status of error, that means this component will be visible to us. Let's reload, see the loading is visible, I will click on success, it is visible, I will click on error, it is visible. So, this is how we can see our work happening even after mapping. If you can see it in code, then don't get confused. This is how we can work. Definitely. All the syntaxes and methods I have told you have their own advantages. You just have to keep some things in mind. You have understood that we are using the same object as we normally use. And whatever option you have here, that option will be coming here. So if the status of loading is loading, then loading will be seen, error will be seen, success will be seen, success will be seen. On the basis of key, your value is being given to the object. So this is our mapping concept. Now I will tell you some things so that you can decide when to use what. So, first of all, you see the mistakes here. So, you don't have to do all these mistakes that beginners do mostly. Writing off inside JSX. What is written here? Okay, writing if inside JSX. If else statement you don't have to use inside JSX. This I have already told you. Nesting too many ternaries. You don't have to nest too many ternary operators. Forgetting to handle else case empty UI. Okay, you don't have to forget else case. Either you have to return null or if you want some UI, In alternate option, you have to show that. Otherwise, you can use and operator if you don't want to show anything. Return null. That is also the right way. But don't forget else case. Otherwise, there may be chances of bug. And then using double equal to instead of triple equal to. You use double equal to a lot of times. Which creates a little unexpected behavior. Here you will know that triple equal to is strictly checking us. And double equal to is not checking strictly. And I had given you an idea about internal working in JavaScript. So, remember that you are using triple equal to most of the time and not double equal to. You are checking strictly so that you don't have any unexpected behavior in future. So, you are using triple equal to here. Beginners mostly use double equal to instead of triple equal to. So I have written that you don't have to make all these mistakes. Forgetting return null. I have also told you that if necessary, then return null here. If you don't want to render anything there, then definitely return null. Don't leave it empty like this. Alright. So, what all things you have to do, take some tips. Extract conditional logic into small functions. Okay, whatever conditional logic is, you have to break it into small functions. Try to make the code modular. Try to keep the logic separate. UI should be separate, logic should be separate and mostly you can use the function to make it modular. Whenever we do a project, you must be getting this idea. Keep JSX clean, logic outside, UI inside. In JSX, try to keep UI as much as possible. And you can definitely use a little bit of ternary operator. But the major logic, try to keep it outside JSX. Okay. After that, prefer readability over compactness. You will give more priority to readability. That means code should be readable. Not that you are using ternary operator to make code small. You are nesting it. So, code will be small. But it will look very bad. It will not be readable. No one will be able to understand. So, you have to focus a lot on readability. So that if your code tries to understand any other developer. Then he will be able to understand your code. For multiple conditions use switch or mapping. So, in normal cases, where you have 1-2 conditions, you will use ternary operator. If you have multiple conditions, then you will use switch and mapping. Rest, if you feel that you have to keep UI and logic separate, you can use function etc. After that, you can use variable. And again, you can use mapping etc. So, all the things could be done in conditional rendering. I have told you almost all the things. And Pain depth is a little bit messed up. So. It can be a little bit. That I should explain more. So I couldn't explain. So for that. I can't do anything right now. I will definitely see. That our. As soon as possible. Again. That one more. Paint tab comes. And then. I am able to explain you well. But for now. So work like this. And I think. I explained everything properly. Without paint tab. Right. So. It must have been fun. I hope so. And. Thank you very much. That's all for now. And see you in the next lecture. Till then, bye-bye.