- Podcasts
- ReactJS Developers Podcast
- 5 Essential Checkbox Features in React JS | Handling Checkbox State and Events
5 Essential Checkbox Features in React JS | Handling Checkbox State and Events
ReactJS Developers Podcast
- December 3, 2026
- 1 h 9 min
- Episode 34
About this episodeCheckboxes are common elements in modern web applications, especially in forms, settings pages, registration systems, and user preference…
- Creating checkbox inputs in React JS.
- Managing checkbox state with the useState Hook.
- Handling checkbox events using onChange.
- Understanding controlled and uncontrolled checkbox components.
- Using the checked property correctly.
- Handling multiple checkbox selections.
- Implementing select-all and deselect-all functionality.
- Dynamically updating selected items.
- Using checkboxes in React forms.
- Best practices for checkbox validation and state management.
- Beginners learning React JS.
- Frontend developers working with interactive forms.
- JavaScript developers transitioning to React.
- Students practising React form handling.
- Developers building registration forms, dashboards, and settings interfaces.
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
5 Essential Checkbox Features in React JS | Handling Checkbox State and Events
We are continuing to react again. And now I don't feel good to say that we are continuing to react again. I took a very long break this time too. Actually, my health got worse. It's still not that good. But I can make a video now. Definitely, I am feeling so fresh. So, my health got worse. That's why I got very late. Before this, I took two long breaks. And this time too. So, some situations get created due to which I have to take a break. I don't take it intentionally. Right? But this time I will try. A lot of comments were coming that why it is not getting completed, what has happened, such a long gap. And still people are there. This is surprising to me. There is so much trust. Although trust will be broken, I know that too. But still people were coming on comments, messages were coming that you have to study properly, complete it somehow. So this time, I have taken a complete break of one month, not from react, but from other things. I will focus on all the videos now. Okay, the frequency of the video will be more. I will be uploading videos in a proper way and will complete it quickly. And by quickly I don't mean that somehow, but properly. I have put all the focus on this, so the frequency of the videos will be more. It will be able to be completed quickly. Okay. Rest, unconditionally, something happened. So, again, I can't do anything. Rest, it shouldn't happen. Rest, let's move on. Right? And if you want, I can say sorry. I can say sorry. I am so sorry. So, we have to checkbox today. Right? We have to checkbox. And revise it. If there is a break in between, revise JavaScript. It's not that you are very relevant. You have done all the things in one go. And in one go, you have completed all the things. JavaScript is done. Projects, I am not able to bring them. Do projects till then. There are many other platforms, read from them. Right? I have also suggested many times that there are many other platforms, don't become so dependent on one platform. You can read from there, from other platforms. You can make projects. At least if you can't read, then you can make projects. Right? So, we are going to do checkbox today. And there are many things in the checkbox, so I thought it is very important to tell you. I will also tell you what things are there. Controlled component I told you in the last lecture. Okay, I remember this much. I also saw that yes, actually I told you the controlled component in the last lecture. So, you can see the controlled one, what actually happens, it is linked to the state, whatever input fields or input boxes, so we call it controlled component. Rest, you can go and see. So, here we will talk about control checkboxes. That means, it should be linked to state. I will tell you everything now. In future, I will show you what are the possibilities, what are the scenarios which we have to discuss. Because making checkbox is not a tough job. Checkbox has a lot of logics. Logics means, here if you want to make single checkbox, then definitely you will make single checkbox. But if I say, if you want to make multiple independent checkboxes, then you will try this to an extent, this will also be made. Then if I say, if you want to group checkboxes, then how will you do this? I gave you a lot of ideas. In the previous lecture, I gave you that you can use object or array. So, you should know both the logic. Somewhere, you will be using object and somewhere, you will be using array. Then, how can we make the functionality of select all? How can we make the functionality of deselect all? And all these things are actually used in the project. Select all, deselect all. You must have used it in mobile also if you are using application. You must have seen it in many places. Handling checkbox inside a form. How to handle checkbox inside a form. This has been repeated twice. It will be repeated only once. Okay. Both are the same things. How to handle uncontrolled checkbox. So, this one too. In the previous lecture, I told you how to handle it using user reference. If you want, I will tell you in this lecture. Rest, if you try it yourself, then it is better. It is not used anywhere. I told you in the previous lecture. It is not used anywhere. In a very rare case, if you have There is no need to link with state. Only then you can go in uncontrolled way. Otherwise, you will go in controlled way. So, shall we start from basics? Starting from starting. Very good. Let's start from starting. If you have forgotten something, then I will revise it a little. And revise it too. So, here I have made a project of VEET. I am using Tailwind CSS. I have set up all the things. You must be knowing all this. So, VEET. Project app install, tailwind setup. It is possible that if there is a need for CSS, then I will use tailwind. I will not go to make external file. Okay, so here it is working properly. I will show you. Hello and BG Red is done. Okay, so what to do now? Now we have to make a checkbox. First of all, how to make a single checkbox? We will see that. Then we will see how to make multiple. Then how can we group? We will see that. How can we make select all functionality? We will see that. We will see everything. But first let's see how to make a single checkbox. So here input and here type you have to give simply checkbox. Checkbox. Suggestion. Okay. Type checkbox is here. What else will we do? Nothing else. Checkbox is made. This is how our checkbox is made. This is normal HTML thing. See our checkbox is made. I am able to tick and I am able to untick. Can you see? Our checkbox is made. And control shift type is same. I will also open the console so that if I have to show you something here in the output that what is coming, then I will show you. So this is so much better and this is zoom. So our checkbox is made. I also give the label. You all know how to link the checkbox with the label. So I gave the label and I put this checkbox inside this label. And here I give its name in the label. kuch bhi naam dedo aap, kya naam dena chaate ho, is, check, terms and conditions, tika, terms and conditions aap tick karte ho na, toh yaha pe main deta hu, terms and conditions, conditions, alright, Terms and Conditions. So, you can see it here. So, you can tick here. And if you click here, then it is not ticking. I want to click here too, so it works. So, this is a normal HTML thing. If you want to attach input and this label, then you have to put the same value in ID and HTML4. Like I give ID here. Checkbox. CB. and I give checkbox in html4 as well so if you give same checkbox then both will be linked and if you click on terms and conditions then it will be unticked and if you click on terms and conditions then it will be ticked so checkbox is working properly this is normal html thing now comes controlled checkbox we have to make and work on it so controlled means link with state simple means this only you have to see in detail you can see in previous lecture So controlled means that it should be linked to the state. So definitely we have to make a state. We have to link the checkbox with the state. Okay, so I have made a state here. Const is checked. It is checked or not. There can be only two things. We will not type any value here. So either it will be ticked. True. Or it will be unticked. That means false. It can be ticked or not. So is checked. Comma set is checked. And is equal to, useState, okay? So by default, I put false here. So will it be false? Yes, it is false. No, it is not like that. If I put true here, will it be true? Absolutely not. You have not linked yet. How will it work like this? Until you create your logic here, how will it work? So here you have made a variable, a state. Now you have to link it with inputs. How will you do it? So definitely you know, in the last lecture you saw that you use value attribute here. But here you don't have to use value attribute. Here, listen carefully, you don't have to use value attribute. Here, you have to use checked attribute. If you are giving true in checked, then it will be ticked, you can't untick it. And if you are giving false here, then it will be unticked, you can't tick it. So, to link with state, I will put this value in checked attribute. Now, in checked, it is true. So, if it is true, it will be ticked. Now, I have linked with state properly. Now, I just have to change in state and our work will be done. So, what will we put here? Again, we will put onChange. You already know this. You have seen this in the previous lecture also. We used to put onChange in the input field also. So, we will put onChange here also. Okay. OnChange. So, in onChange, you can make a function here. You can make a function here also. You can make it separately. You can pass a reference. So, there are many ways. So, I want E here. Event. So, I will definitely have to make a function here. Okay. Let's make it separately. Let's make it separately. const on change Here we will get an e. So this function, as soon as we pass reference here, we will get event object. I have told you this in the last lecture. So the event object that we will get, here we need e.target. What do we need? Value? No, no. We need e.target.checked. e.target.checked. We are not using value here, we are using checked. I am saying again and again that we are using checked attribute. So instead of e.target.value, we will use e.target.checked. And from here our true and false will come, whatever changes are happening here. So now I have fixed the value. I have given isChecked in checked. So it will always be true. I will console here and show you. I have already consoled here. I have already consoled in onChange. So, I will tick here. So, see here. False is coming actually. Why false is coming? It is false because as soon as I click, it is false in e.target.checked, but I have given true in this checked attribute, which is true, so it is true again and again. If I remove it for the time being, you will know what is happening here. As soon as I click on it, it becomes true. And as soon as I click again, then this E.target.checked will be false. So it was coming here. Because earlier it was ticked, so I was ticking it again, so E.target.checked was coming false. But because I had given this one in checked, so there it was checked. So it was looking different, the value was different. Okay, so now here it is doing proper work. True, false, true, false, true, false. But I have to link with state, so definitely I will I will let it be checked like this. So, now it will remain true. And if I click here again, then it will want to be false, but it will not be possible because we have not changed in this. We have not changed in state. So, definitely we have to change in state too. So, in set is checked, we will do e.target.checked. Okay. Now, see what will happen carefully. Now, I will console the state here and show you. CLG is checked. Okay. So now if I change it to ON, it will be ticked. It will be unticked, i.e. E.target.checked will be false and false set. As soon as false is set, this check will be updated wherever it is used. You know all this. So here it will be unticked. And when you click again, E.target.checked will be true. So in this set, you will get true. This check value will be true. So if it is true, then it will be updated everywhere. So here it will be true and it will be ticked again. Okay. So, this way it will work. So, I reload. By default, it is ticked here. I have given true value. Now, if I click on it, then see, it is false. I will click again. See, it is true. And it is coming twice because again, come to main.jsx. Remove strict mode. Okay. So, it will not check twice. So, here, now, I tick false, true, false, true. So, properly, you can see it on UI and here also. And it is getting selected. On clicking again and again, this blue blue line is coming. So, select none. This is the work of CSS. Let's make a div. In the last name, select. We will give none. So, now I want to do anything here. Select is not done. So, we are ticking on it. It is working properly. And it is also linked to the state properly. So, we call this control checkbox. Okay. Properly, it is synced with the state. That means, this is our control checkbox. And this is a single checkbox. If we want to make multiple independent checkboxes, which is our second scenario. Here, if you come and see, then you can read this. I have given proper notes as always. I will put it in the description. So, here, our single checkbox is done. If we want to make multiple independent checkboxes, then we will make multiple states. We will make multiple checkboxes. That's it. It is very easy. There is no tough work here. If you want notes, then I will copy it I will make a notes folder and in notes I will write single check box dot js not js but jsx so this is our single check box now I will show you multiple check box So, if you want to make multiple independent checkboxes, then nothing. Multiple states will have to be managed and nothing else will have to be done. So, here 1, 2, 3. Assume I can take 1. Is HTML, is CSS, is JavaScript. Take it. Is HTML, is CSS, is JS. Okay. Language has to be selected. HTML, CSS and JS. So, here also, this will be ours. Set is HTML. Set is CSS. So, quickly, I will copy it here. JS is JS. Okay. So, now, the first one which will be ours, this will be ours for HTML. So, if you know how to manage state, if you have learned so far, that's it, you know it. Here, I will beautify it in a proper way. Now you must be able to understand it well. So here instead of this check, I will give this HTML. Instead of terms and conditions, I will give HTML in the label. So I will tick on HTML. So here our HTML work will be happening. So this one, now here I will have to make a function again and again. It will take a lot of time. So I will do one thing. This whole work, I will do it here by making a function like this. By making a fat arrow function. and when to call, when to pass reference, you have got the idea, definitely, so here is html, and here is e.target.check, here set is check, sorry, set is html, right, E is not defined. Why? Okay, we haven't passed E here. So, now it is working properly. So, we are getting HTML. If you want, you can show it like this. Here, use a BR tag. Line break will come. And below, you can show it by doing conditional rendering like this. If it will tick on HTML, that means it will be true. So, if it is HTML, then show it here. HTML selected. Right? If HTML is true, then you will see HTML selected. I will deselect the selected checkbox in this way we can handle multiple independent checkbox HTML selected CSS selected Here we have our js selected. You do it yourself. This is all very easy. Just replicate it this way. HTML, CSS, JS. This is our HTML above. And below this is our CSS. So do CSS here. Do CSS here too. Here you will get this CSS in value. And here what to do? Set is CSS. Okay. After that do CSS in label too. So that CSS is visible too. And here you will get JS. and same here too js and here is js very good and here set is js ok so according to me I have done all the changes so you don't have to do anything different you have made it like single you just have to do the replication work ok by changing the values so see all of them are ticked because by default I have kept true so I will remove this so see this is removed and here if you want then give one line break here and give a line break here. So, you will like it. Rest, you can style with CSS or Tailwind, but that is not our topic now. So, JS is removed, CSS is removed, HTML is removed. CSS is here, JS is here, HTML is here. Okay? So, in this way, you can handle multiple checkboxes, multiple independent checkboxes, by making multiple states. Now, we use the previous scenarios more. In this way, we do not manage multiple states. If there are multiple checkboxes, So, we prefer to group it. If it is of similar type. Like here, all the languages were there. HTML, CSS, JS. Okay. All the languages were there. So, we can arrange it in a group. We can combine all of them. How will we do it? So, let's see now. It will look more simple to work. So, here our next day is group checkboxes. Okay. So, group checkboxes, dynamic handling. Wait a minute. Take pen. Okay. Take this one. So, what we did here, multiple, gone, pen tab again. Oh man, there are a lot of expenses. Leave it, move it. Okay, so here, multiple independent checkbox, we did like this, we just have to group it. So, how will we do it? You tell me. In the last lecture also, I told you something like this, that with a single function, everything can be handled. All the checkboxes. think and see how you will do it. I will tell you, see. If you have come to your senses, then definitely do it yourself and see, rest I am telling you here. So, I will copy here and I think I have to take a break. This cold makes the mind bad. So, single checkbox has been made. What was this now? Multiple checkbox dot JSX. Isn't it? This has come, multiple checkbox.jsx. And now we have to group this. So how will we group? So it is useless to make multiple states. Make multiple states and handle multiple states. See, we had to make different functions here. Here was a different function, set is HTML used. Here was a different function, set is CSS used. Here was a different function, set is JS used. Whereas logic is almost the same. So why not group? Why not group? Work in single function, work in single state. What can we do? So we can make an object like this. I have told you something like this in the previous lecture. It is very important to revise. So you must have done it definitely. So here I will take HTML, CSS, JS languages for the object. So languages, set languages is equal to useState. And here we will make an object like this. We will put everything in one object. HTML, by default we keep false. CSS by default is false, JS by default is false. Now our state has been created. Now see carefully what changes we have to do. We just have to do so much changes that in checked, everything else is fine. Here we have given the link to link HTML4 and IDE, type checkbox will remain. We just have to change two things. Okay. Two things. Only two things. First of all, this onChange, basically function. And second, this checked one, which we have given value here. So, how will value come? So, value is an object. So, we will access it with dot notation. So, languages.html, languages.css, languages.js. Okay. So, here, languages.html. Then, here, our will come. Languages.css. And here, our will come. languages.js. So, we have linked to the state properly. Now, what we have to do is, we have to work on the function in this way, so that all the work can be done in one function. Okay. First, see how to change. I will tell you after doing it. First, pay attention to how to change. So, what we were doing here, we don't have to do this work now. Now, what we have to do is, we have to do set languages. Okay. We have to do set languages. Now, set languages is an object. there is an object, so we can't do direct html, false, css, all these things, so how to work in object, you also know that, here in set languages, we will spread previous dot dot dot, previous object, means whatever is in languages, and after spreading, what we have to change, in this check box, we have to change html value, so here we will give html key, and e dot target dot, checked means if all the other checks are unchecked, suppose here CSS is true, JS is false, now we have to click on HTML, then what should be changed by clicking on HTML, only HTML, so I spread the rest here, the rest will come here as it is, I just changed HTML, I gave HTML, that means HTML will change, the rest will spread as it is, if the value of CSS is true, then it will remain true, if JS is false, then it will remain false, what will be the change here, the value of HTML, And this will become a complete object and will be set in this language. The state of the object will be set. And here we are not directly manipulating the object. You must be knowing all these things. We have to avoid direct mutation. We have to work by making a copy. So, the spread operator is making our copy. Alright, so we should do this work. error will come because we haven't done it in sub so let's do it in sub let's do it here also let's do it here also ok and here instead of html you will get css and here instead of html you will get js that's it so now this should work but still error is coming is html is not defined ok where is html ok what is this so here is html not is language not is language, language is, if dot html is true, then our html should be selected, and in the same way for css and js, css and js, okay, now let's see, is this working, js I ticked, ticked, unticked, unticked, okay, so js is being selected, css is also being selected and selected, html is also being selected and selected, So, it is working properly. But, it hasn't happened yet. If you see here, the logic has become very long. Logic in the sense, here in onChange, see, we have done this much work. Here also, we have done this much work in onChange. And here also, we have done this much work in onChange. And everyone has done the same work. If you observe carefully, everyone has done the same work. In onChange, we are taking E and doing set language. And in set language, we are spreading the languages. We are spreading the values of the old languages. And here, we are just changing the HTML. Rest, e.target.checked is also the same. Here, html key is changing, here css key is changing, here js key is changing. Just to change this much, we are replicating the function here again and again. So why don't we make one function and try to do all this work with it? How will we do it? So you know, we can use dynamic key here. You must have seen in the previous lecture too. Here, if I make onChange, ok, onChange, I'll make a function, const on change, let's write handle change, handle change, and here you'll get e, okay, you'll get e, so can we, can we take this key, all of us, We just need to change the key. How will we take the key? I will give a name attribute here. In the name attribute, I will give HTML. The key should be given carefully. What is the key? It is HTML. So, I have given HTML in the name so that I can receive HTML here. Here, I will give CSS in the name. Here, I will give CSS in the name. And here, I will give JS in the name attribute. Now, what will happen with this? It will happen just this much. Look carefully. I'll show you using CLG, e.target.name. Whatever attribute you're using here, you can receive it like this. So I used the name attribute. I'll use it as a key, in a dynamic way. Here, if there's HTML, there'll be HTML. If there's CSS, there'll be CSS. If there's JS, there'll be JS. Okay? In the previous lecture, I told you in detail. So now you can see if there's even a little bit of confusion. So here, wait a minute. We'll have to put handle change in front. So, Now, we will remove all this garbage. And we will use the same function in all. Same function in all. Same function here too. Okay. So, let's see if we are getting the key. We clicked on JS. See, our key is coming. JS. Here, we are clicking on CSS. So, CSS is coming. We are clicking on HTML. So, HTML is coming. Okay. The name attribute we gave, we are receiving that. So, this is coming to us. Okay. This is coming to us. So, we will save this as a key. Const key is equal to As a key, we have saved it. Now, what we have to do is set languages. Again, the same logic we have to apply here. Set languages. Dot, dot, dot. Previous languages will come here. And there is one more syntax. You must know that. Functional update. You can do that too. Dot, dot, dot. Languages will come here. and here key should be changed. If I am clicking on HTML, then the value of HTML should be set. If I am clicking on CSS, then the value of CSS should be set. If I am clicking on JS, then the value of JS should be set. So, how will it be known which one I am clicking on? It will be known by key, e.target.name. If I am clicking on HTML, then here HTML will come, CSS will come, CSS will come, JS will come, JS will come. Rest, it was the same in all, e.target.value. Is it or not? So, we have to work in this way. Our work is over. Come, let's see. Let's reload. See, the JS one was true. The JS one was false. Okay, it wasn't false. Why wasn't it false? It should have been false too. E.target.value. It should have worked properly. CSS, HTML. Reload it once. No, it's not unticking. Why? So, according to me, we should put a console here. So that I can find out that the value is not being set properly. So let's console the languages. I think I have done the rest of the things right. So let's clear it. I clicked on JS. It is not being unticked. Okay. See here. In JS, we have on and off. Okay. We have considered the value. Okay. I was saying again and again that it has to be checked. And I have done the value myself. So here, always keep this in mind. E.target.checked. You have to check it. That's why in Boolean, true and false. So, now it will work properly. See this. Okay. Great. So, in this way you can group. This single function is yours. Right. This single function is yours. This single state is yours. And with that you have done all the work. Did you enjoy? You must have enjoyed. Definitely. You can also do mapping. Okay. You can also do mapping work. That logic you have to think that how can it be sorted by mapping. In many conditions we also map. If We can do mapping too. But again. But again. Okay, I believe you have the mapping too. Select all, deselect all functionality. We will make all these things. So definitely, we will have to do map also that's why we will be able to select all together deselect all right so let's continue this also select all deselect all so everything will be covered don't worry everything will be covered map will also come in this will be covered ok so in notes I will put you here single checkbox multiple checkbox What was this? This was our grouped checkbox. I have told you all the possibilities here. More than this, I don't think there will be anything else in the project. If something else happens, then you will make the logic. Right? So this is ours. Now the people who are going to work here, they are going to work on the select all, deselect all feature. And for this, give me a little break. Because I am not feeling so much better. Okay, so now let's make the functionality of select all and deselect all. And it is also very easy to make. You don't have to do anything too complicated here. Here, what we have done in this way. Here. So, to select all here, we will make another checkbox here. Let's copy this. In this way. Whole. and at the top select all select all alright so do we have to create a separate state for this? not at all no state has to be created think for yourself how select all works how it works if I click on select all So HTML, CSS, JS should be selected. That means I just have to see that if all three are selected, then the selected all will be ticked. And if all three are not selected, then the selected all will not be ticked. That's it. This select all is dependent on these three. It doesn't have its own data, it doesn't have its own state. It is dependent on these three. So is there any need to make a separate state? Not at all. This is how you have to understand that select all means not only this, whatever you do in the future, this is how you have to think about the logic. Do we need to make a state here? If all three are selected here, then select all will automatically be checked here. And if not even one, then select all will not be checked here. So it is dependent on these three, it has nothing separately. So we have to do something here. So first of all, we have to check if all three are selected or not. So how will we check? So to check, To check, here, if there was an array, then we could easily traverse it, basically, go to each item, go to each value, and see if all of them are true or not. Okay? By mapping, you can understand this way. So, if there was an array, then definitely, the object can be made an array. So, the object can be made an array in two ways. One is the array of all the keys, and the other is the array of all the values. So, which array do we need? Key array or value array? We need the array of values. That's why you can check if all of them are true or not. Or if any of them are false. So we need the array of values. So let's make it here. Down here. So array. Not array. We have to make an array from object. So object.keys. We have keys and a value too. Type it here. Values. So object.values. Whose values? Of this object with languages. Okay? So, I have put languages in it. So, what will it do? It will give you all its values in an array. So, const, const, here I will also show you, what should I name it? IsAllChecked. This is what it is going to check, right? All of them are checked or not. So, I will do CLG, IsAllChecked. IsAllChecked. So, you see here carefully, you must have an array coming. I will reload it. See, an array is coming. false, false, false. Now I have to check whether all of them are true or not. So whether all of them are true or not, if such a condition is checking us, then what do we use in JavaScript? What do we use? We can do map, forage, anything, but what is proper here? Proper is our every. Okay? What do we use here? Every. Rest of the conditions are different, but here the easiest is every. What every does is, if all of them are true, then it returns a single Boolean value, true. And if it is not satisfying even one condition, then it returns a false condition, false. Okay, in what way? So basically, this is also mapping in one way. It is just returning in Boolean, true and false. True will happen when all of them are matching the condition. And false will happen when not even one condition is matching. So, here you will have one item. Okay. Now, you see here. You see here that all of them are true or not. So, item. If all of our items are true. Okay. If all of our items are true. So, is all checked. True will come. Basically, it will be returning true. If even one is false. That means, it is not satisfying the condition. So, it will be returning false. Let's see. Now see here it is returning false. If all of them are ticked. This is also ticked. This is also ticked. And now this select all. I don't know how it is ticked. I mean logic here. We haven't written select all yet. Select all. What is this? So we have written languages.html. That's why it is selected. Comment this for now. On change. Handle on change. Comment this for now. Now it is fine. Reload. So we have selected html. Oh man. Why is this happening? So, this is happening because we have given html here. Select all sa. Okay. Did you see? We clicked on label of someone else. Selected this. So, this is the command of id and html4. These are the things of html. So, now it should work properly. We selected html. See, now it is false. All checked false. We have selected CSS, it is still false. We have selected JS, it is true. Why? Because all of them, all of them, we had an array, object, dot, values, languages, so all of them here, the values in this array, true, false, true, false, true, false, all of them are true, that means our true will return here. If any condition does not satisfy, then our false will return here. This is the small thing. This is the matter of JavaScript. It has become natural. So you can check it in JavaScript properly. I have told you everything, filter, everything in detail. Filter, reduce, every, all the methods are there, everything. So you must go and check out once. If you have not done it, and if you have done it, then it is not a problem, then you must have understood it earlier. We made an array here. We made an array and checked it from every. You can do one more thing here. Direct, if you want to work, then pass boolean here. If you want to pass a function, then if you are passing boolean here, then our java script understands that here, if all the truthy values, truthy value, this is not true false, it works on truthy value. All of them are of Truthy value. That means, all the values except false are of Truthy value. You know this. So, if all of them are of Truthy value, all the items are of Truthy value, then you must be getting this in True value. And if you get even one false value, then you must be getting this in False value. So, see now. Reload it. All are false. One is true. But it won't work because there are still two false. Let's tick this too. Still one is false. All should be true. Let's tick this. All are true. So here you have true. Okay. So this is one way. And the other one I told you. Map it and put condition. All conditions will be satisfied. So every app will return true. If one condition will not be satisfied, then false will return. So this is our way. All are checked or not. To find this. Now, we will put this here. If all of them are checked, then our select all will automatically be ticked. If even one is false, then it will be unticked here. As soon as all are selected, it will be ticked. But, but, but, but, is it happening the other way round? If we are clicking on select all, then are all of them being selected? No. If I click on select all again, see, I am clicking. Are all of them being selected? No. So, how will this functionality come? So, it will change our on. Okay. So, will this handle change function work? No, we have to make a separate function. Definitely. So, I will make it here. You can make it separately. No problem. Make it separately here. Make it separately. Function handle select all. Oh man, I mixed both. I mixed both functions. Okay. Handle select all. And what should happen in select all? Think about it yourself. All the values. should be ticked. If I am ticking on this, listen to me carefully, if I am ticking on this, then checked of this one, okay, from this one, I will get checked, e.target.checked. So, if it is being checked, then I will apply it in all of them. And if it is being unchecked, then I will apply it in all of them. Okay, did you understand my thing? What I am trying to say is, from here, I will take e, e.target.checked, I will get it. Okay, as soon as I click on select all, then check will be unchecked. So, if check is unchecked, then what do I have to do? I just have to do this much, that here you have the whole object of languages. The whole object of languages. Here I have to set it. Like this. And there is no need to spread it. Because here all of them have to be set. So html. And what will happen in html? e.target.checked. Okay. Checked. And in the same way it will come in all three. html, css and here js. So, if it ticks in select all, then apply it in all and tick it. And if it unticks in select all, then untick it in all. Means, we have to take the value of select all, checked and unchecked, and apply it in all. This is select all, deselect all. Now, see here. Let's reload. We have selected all. click again and all are deselected. That's it. It was so easy. All are deselected. All are deselected. All are deselected. If I deselected even one, it is removed. If I deselected even one, it is removed. If I deselected even one, it is removed. If all are ticked, select all is done. So, this was our select all functionality. You just have to keep in mind this one line. This line is very important for select all. Okay. And... It is working perfectly. So, you understood this too. And there is no need to map. But if you want, you can map too. Like you made an array of values here. Similarly, you make an array of keys here and map it. It is not a problem. See, I will make it and show you. This is ours. One minute. Java script is written. So, put curly braces. This is ours. It is an array of values. But you don't have to map on values. You have to map on keys. list all the keys here, html, css, javascript, so list all the keys, so dot map, and here each item will be coming to you, ok, each key will be coming to you, and here you pass this label here, returning is very important, otherwise you will not see this, so remove all this from here, because you are going to map, so if you map, then your work will be done here, ok, see how small the code has become, The code has become so small. So, what you have to do here is, the item that is coming, you have to pass it in HTML4 as well. Right? And here, you have to pass it in ID as well, like this. And here, the type checkbox will remain the same. The name will be different here. Right? The name will be the same as the key. Alright. What will be coming in checked? So, what will be coming in checked? We need the value of each one in checked. Okay? HTML, CSS, JS. So, you can do it this way. One item is coming, right? One item is coming to you. One item is coming to you. So, here you have an object. What is the object? Which object is it? Wait. The object of languages. So, inside the object of languages, if you have to find the value of any item, how do you find it? We will use the array structure. You can also do it with dots. Dot HTML, dot CSS. But here, we have to use this item as a variable. So as a variable, when we have to use, we use the notation with bracket. So here will be item. If item is coming as HTML, then the value of HTML will be visible here. If CSS is coming, then the value of CSS in the object will be visible. If JS is coming, then the value of JS in the object will be visible. So here the checked one is also proper. And in onChange, this handle select all is attached. Alright. Alright. Oh man. I did it in select all. Why do I do this? Why am I like this? Wait. Wait. This is our... We didn't have to do this. We did it in select all. We had to keep select all separate. This whole thing. Pick it up from here. And we had to apply it here. Hmm. remove from here confused? see i will explain now i have to keep select all separate select all has different functionality right? right? say fast select all has different functionality select all has nothing to do with this we are doing this for html, css, js and if there is any other add then we are doing it for that so this will be our html, css, js and this will be our select all right? now did you get it? we did this in select all so select all is this select all has different functionality handle select all and whatever it is, and it is different. So, here we have item, and this same item, we have to put here also, and here also we have to put, and in checked, here in this way, languages, instead of dot notation, we have to do in this way, and handle change will be as it is, alright, and here in label, in label, here, You have to enter the item. If you want to capitalize, you can do it. Item dot to uppercase. Okay. You have to capitalize. If you want to capitalize, you can do it. CSS, JS, HTML. And select all is also working. Deselect all is also working. Everything is working properly. And here unique key prop. How did I forget this? Here. Give the item in the key. Because all of them are unique. So there is no problem here. Wow. Did you understand? It is working properly now. Yes, it is working properly now. So, I hope you have understood this much. I have told you all the possibilities. There is one more. We have done checkbox now, but checkbox is not the only thing in the form. There are other things too. So, let's combine that too once. So, you will be more clear. So, I will give this to you in notes. This was ours. What was it? Oh, man! What was it? Select all, deselect all. select all.jsx ok now we have what else we have inside of form how to handle inside of form ok so let's see here whatever we did we spread variety select all remove this ok I have given in notes so now this whole work mapping etc. remove everything we don't have to map now let's clean it neat and clean first remove return from here remove this also and actually remove this also we don't need this also remove this also not BR, BR handle select all remove this is all checked remove it let handle change and here form data I will do this form data set form data now I have to use combinely here That means I have to use input box as well as check box. So how will I do both together? Because both have some differences. Like in check box, we check e.target. There we do e.target.value. There are some differences. So how will we do it? How will we handle it? I will tell you this once. So suppose I am taking email in form data. And by default, it will remain empty in email. And here I will tick on terms and conditions. Terms Conditions TC. Okay. So, this will be by default false. Alright. So, this is done. Our Terms and Conditions is already done. Terms and Conditions. Right. Here I give inside the label. Terms and Conditions. So, this is our checkbox. Done. Definitely. FormData.TC. Remove key. Key is not needed. Here. In ID, formData. And here in HTML4, formData, TC. TC. And this will be the checkbox. Name will be TC. Alright. Because I want to work with a single function, whatever I want to work through a single function, that's why I have given name here as an attribute. And here in checked, checked, formData.TC. Okay. Terms conditions. And in on change, handle change will remain. Let's make handle change here. It has been made. It has been made. So, this is our checkbox. And in the same way, an input field will come here. Type text will remain. And let's type email. Right. Our type email is done. And placeholder. Enter email. And what happens here? Tell me quickly. How can we make control component from this? So, we have to give value here. So in value, we will give formData.email. So here our value is linked. And here what happens is onChange. So in onChange, handleChange will come. HandleChange. Now how will we handle it properly? Here both are different. Here value is there. And along with that, give name also. Name. Email. So here value is there. And here we are using checked. So how will we do work? How will we do work? How will we do work? So now there will be a lot of writing spread here. See, let's type email on enter email. So set languages is not defined. It is obvious. Where will set languages come from? Here is set form data. So set form data and old form data will be spread here. And what will be changing, that's what we have to show here. So form data. Okay. So key will come from here name attribute. That's very good. That's very good. What else do we need? And checked also. About this, checked also is needed. Value also is needed. So, let's do one thing. Let's destructure everything and remove it. Const, name, value, checked. Let's remove all the things at once. We will use whatever will be used. E.target. So, if we are typing, then while typing, our key will be named. And we have to set the value. But, now there is a problem here. While typing, we have to set the value. But when we are ticking on the checkbox, then at that time, we have to put checked here. So, render it conditionally. What else is the problem? So, how will we do it conditionally? How will we know that there is a checkbox or there is an input? How will you know? So, you will know by its type. Here, if type is email, leave email, not email, checkbox, if type is here, if type is checkbox, then you have to show checked, and if type is not checkbox, then you have to do input box work here. You have to apply logic in this way. So, type, if our checkbox is, copy spelling from here, type, if our checkbox is, then your checked will be going in value. What will be going in value? checked means true or false and if there is no checkbox then the value which we are typing in the value should go that means value this value did you understand what I said so in this way you can handle the form let's reload here we are typing so type is not defined type is not defined ok we didn't take type from here you also get type attribute here so take type so if type is checkbox then checked otherwise value ok What else should we do here? All definitely. If it is form, then wrap it in form. We will do it later. First let's see if the value is coming here or not. Let's console here. CLG form data. Right? So now we will know that it is not doing proper work. I clicked on terms and conditions. Form data. What problem did it face? Set form data. I went to form data. Here. Reload it. Function is showing it. Form data, set form data. Okay, I have written capital form data. Wait a minute. Form data. This form data is something else. Here we have to put this form data. Form data. And we have made a lot of mistakes. Here also form data will be there. Here also form data will be there. And here also form data will be there. We had put the capital F form everywhere. Here will be the form data. Now this is fine. Let's check once. Is the form visible anywhere else? No, it is not visible. Form. Form. Okay. So here you have terms and conditions false. I have ticked. Terms and conditions are true. And as soon as I type, it should be set in email. It is set in email. I have false it. So terms and conditions are going. Okay. So in this way, you can make multiple lengthy forms. You can create multiple elements in the form. And there is one thing that you will definitely know. This is an HTML thing. Which is that you can wrap it in the form. Everyone will know this. If it is a form, then you can wrap it in the form. Here you have a submit handler. On submit. And in on submit, you can run submit handler. Submit handler. And you can make this submit handler up here. CLG. Not CLG. Function. Function. Submit handler. Like this. And CLG. Form data. Alright. So now whenever I do something here. And submit. So we didn't make the submit button. So make the submit button here too. Button. Submit. Submit. Alright. And here. Submit. Okay. So, let's reload. Enter here. We have hello at the rate gmail.com and clicked on terms and conditions and did submit. So, here we got data and it also disappeared. Why? Because here we did not prevent default behavior. I had told you all this. Is it not? So, e.prevent default. Spelling is written correctly, isn't it? If it is correct, it will work. If not, it will not work. Let's see. So, this at the rate gmail.com I have submitted. Yes. So, email is coming to you. And terms conditions are coming true. Because we have ticked. So, you have to handle this too. So, in multiple ways, now you can handle complex to complex forms. Two more things are left. Radio button and select list. There is a list. You select from this. So, that is left. Select option. So, we will do that too. We will do radio button too. But in next lecture. In this lecture, we have done it. So, we have done the form one. Use ref one, you can see it yourself. Its use is not going to come. Rest, the previous lecture, I told you, uncontrolled checkbox, uncontrolled input box, as we were doing, it will be in uncontrolled checkbox also. What will be the difference? Checked instead of value. That's it. Okay. You will use reference. As you set reference, in input box, as I told you, you will set reference here also. And reference dot current dot, If you were doing value, then you will check here. That's it. There is no other difference. So, this one is your self-task. You can do it. Okay. Even if you don't do it, it's not a problem. It's even more important. If it comes in input, then it comes in this. If it comes in input, then it comes in this. It's the same. Just check it. Rest, uncontrolled means, you are understanding, it will not be linked to state. If you want to work without state, if you don't want to have anything to do with state, then you sometimes, in rare cases, use uncontrolled checkbox or uncontrolled component. Otherwise, in React, you use controlled component everywhere. And recommended is also that you work by making controlled component. Okay? So many things you understand. If you understand, then here is a summary given. Single, multiple, group, select all, inside, form, and uncontrolled. Whatever is here, you can read the technique. and read the type of state, and here I have given an example, you can read that there is boolean, we take multiple boolean, we can use object and array to group, okay, I didn't tell you about array, I didn't tell you about array, that you can use array to group, so I will tell you this too in one go, okay, you can use array to group, so what was this, this was form, okay, check, box under form.jsx. Okay. So, this is done. And quickly copy the group checkbox from here and paste it in the app. Okay. And I quickly tell you the array one too. How will you do the array one? So, the array one is also not very complicated. It is simple. If I tell you the logic, what will you do? See, what will happen? Here, if you have 5 or 3 checkboxes, HTML, CSS, JS. So, the one you tick, you have to put it inside the array. The one you tick, you have to put it inside the array. Okay? And if you untick, you have to remove it from the array. And you have to check if it is present inside the array. Array.includes. If it is present, then definitely it will tick. And if it is not present inside the array, then it will untick. I will do it. See, it is very simple. So, here, without making an object, you have to make an array. Without making an object, create an empty array and whenever you change the handle here then what you have to do is set the language here but this time there is no need for a key there is no need here you have to set the language here and whatever previous value you are getting here you can use functional update here do functional update or you can do it like this But you can also do it like this. But here you have to apply more conditions. That's why I was telling you to do it like this. See, I'll tell you like this first. Then I'll tell you in a functional way. So what you have to do here is, if there is already a present in the array, like if there is an array of languages, then in the array of languages, languages.includes includes e.target. e.target. name. If html is already present in it, then we have to remove it. And if it is not present, then we have to add it. That's the difference. If it is present, then we have to remove it. If it is not present, then we have to add it. If it is already present, then we have to remove it. And if it is not present, then we have to add it in array. It is very easy to add in array. not much, dot dot dot, the old one that you have, spread the old values, and put the new values here, e dot target, dot name, name will definitely go, name is being treated as a value here, a value here. If you want, you can set value attribute instead of name in this way. And you can work on value as well. It's not a problem. Instead of name, you can work on value as well. Basically, here, use name attribute and receive it. Use value attribute. It's your choice. It's your choice here. You are definitely able to understand. So, let's take value. So, it will be a little more clear. So, where to use value, where to use check, you will understand. So, value here you have js and value here you have css. Spelling etc. Look carefully and write. So, e.target.value If it is already present in the language, then you have to remove it. And if it is not present from the beginning, then you have to put it. So, if it is not present, then it is dull. How will it come out? To remove it, definitely you have to filter it. And you can do this easily. So, let's go inside array. We can't put directly, right? We have to set here. Set languages. Like this. Okay? We will set with set languages. We will put new value. Now, what can we do to remove? To remove, again we will do set languages here. and here you have to use filter from languages that's why I was saying that in one time you don't have to do set language twice, it will be done in one time so use functional update here, it will be better set languages, here you will have previous languages languages you have read functional update and here you have to definitely return and here you have to write your logic that if in previous languages include is not a language then something and if it is something then here e.target.value ok if it is present then work to remove or work to put so to put nothing much just here you have to return an array dot dot dot dot previous languages if javascript is strong then all this is very easy for you and if javascript is not strong then it is very complicated for you So, previous language is here and to add a new language, e.target.value. I will explain it once more. Wait, let me do it first. So, in previous language, include is here. So, there was to be an output work here and if not, then there was to be an input work. So, we have done the input work. Now, how will we do the output work? So, previous languages, which is an array, we will filter from this. If the filter is correct, then we check which elements to return and which not to return. So which elements to return? You tell me. You have to leave e.target.value and return all the elements. Now how do I explain? I don't even have a pen tab. Assume it has H, CSS, and JavaScript. Okay, you ticked on CSS. It was ticked before. click on it again and it will be unticked now to be unticked we have to remove it from here now what will we do to remove it so to remove it we will remove all these things html js will be removed and c will be left here we will not filter it out that means it is removed our html and js came in the new array and c is removed so we will put a condition like this here so our condition will be something like this that we will be getting each item here and item If our not equal to is of e.target.value, then we will return all those elements. That means, we will remove HTML and JS, we will make a new array and return it. Okay? We will keep CSS. We will not remove CSS. So, our CSS filter is out. That's it. So, filter is not coming. It's a problem. This is all basic JavaScript. Right? It's not too basic. It's a little advanced. We can call it intermediate JavaScript. Alright. So, will this work for us? So, this is our onChange one. Now, how will we find out if checked or not? So, same process. Languages.includes. If here HTML is included, then it will be checked. Otherwise, it will not be checked. That's it. Okay, here we will get JS. And here we will get CSS. And here we will get HTML. So if our language already has HTML, then it will be checked. If not, then it will be unchecked. And if we basically click on onChange, then it will be added and removed. If it is already present in it. Okay. So you understood this logic. It was a simple logic. You have to add and remove. And you have to check if it is included or not in it. So here we have an error. Unexpected token set languages. So we have made some mistake here. Previous languages, everything is fine. We have returned here. We are doing fine, right? What is the problem here? Previous languages dot includes. If we have set language. Okay, here. Use state. This syntax is wrongly written. Okay. So, let's reload. So, our js is ticked, css is ticked, html is ticked, unticked, but we don't know if it is going or not. So, how will we know? So, here you can see it by doing clg. js is here, css is here, html is here, html is here, js is here, css is here. Rest, in the same way, you can also do conditional rendering here. If language.includes css is there, then you show css selected. if there is html in languages.include then show it as html selected otherwise you don't do rendering like this you take the data in a different way basically these are all the things and send it in backend so there are multiple ways you may be getting this use this is more beneficial to send in database if we have to send array only that we have selected all these elements we have to select all these elements and send it in database then definitely this method is better than object And if you want to receive the backend engineer in the object way, then you can send it that way too. So all the ways I have told you, there was no such method which is not used in practical projects. All of them are used. All of them. We use all of them. All the ways I have told you. Except one. And one was this one. Uncontrolled one. Rest, we use all. This too, in rarest case, you can see it sometimes. Rest, you won't see it. Okay? So, I hope you enjoyed it. You should come. Tips are, here, checked is used. Value is not used. Don't use e.target.value because here, you have to use e.target.checked. If you use value, then on, off will be available like this. Reset checkboxes easily with setState. Definitely, you can reset checkboxes because you are playing with state here. So, you can reset all the things at once using setState. Keep logic pure. Don't mutate array or object directly. I have said this in almost every lecture that array and object should never be mutated directly. It should be copied and used. So, this is how we were doing it. And mutation, what is mutation, I have told you all that in JavaScript. And in this lecture, in this series, I have told you somewhere, properly. Group checkboxes if they belong to one category. And try, if there are checkboxes of same category, then group them. So, these are tips for you, so that you can make better application. And thank you so much. And sorry for coming late to the video. Very, very much. It happens. Okay. So, let's meet next. Even now, my health is not that good. I don't know. I can't tell by the sound. But, it's cold. If I do this again and again, it won't feel good. That's why I am not doing it. I am stopping it. Isn't it? So, I hope you enjoyed it. Then, we will meet in the next lecture. We will discuss radio button. We will discuss select. And, we will move forward in more complex topics. Now, we have a lot of topics. Our core react is going to be completed. We have a lot of topics left, like XOs, API, etc. After that, we can make good projects. State management, Redux toolkit, Just Stand, etc. So, I will try to teach Just Stand, Redux. Because in companies, it is not necessary that you have Redux. It depends on the project. If you have already done Just Stand in the project, then you will have to do it. If you have done toolkit, then you will have to work on toolkit. And now you don't understand. I know. Because you haven't even heard the name of it. But you have heard it now. So I will definitely tell you all the things. Don't worry at all. So keep going in sequence and flow. Your flow is fine. Mine breaks. I will continue now. So that's it for now. Till then, keep exploring. Keep revising. Keep reading properly. And don't be too dependent. And thank you so much. ki aap abhi bhi continue kar rahe ho lecture. Otherwise toh, kaun hi dekhega itni time baad. Hai na? Thank you.
