I've learned this concept before from the React docs and it's called state as a snapshot. When we write setCounter(counter+1) multiple times it is updated only once because in the onclick handler of that render, even after calling setCounter(counter + 1) multiple times the value of the counter is 15, because after our event handler finishes, then React re-renders the component. They explained very well in their docs but you also explained very beautifully sir ❤❤
@heelsadhna9 ай бұрын
but if i did setcounter(++counter ) for multiple times then value increases in multiple . kindly provide me the reason
@MuhammadAbdullahMudassarMalik9 ай бұрын
@@heelsadhna ++counter in React is not recommended. since you are using Set Method.
@codingwave564 ай бұрын
@@MuhammadAbdullahMudassarMalik But why it works ?
@ProgrammingPlanetАй бұрын
@@codingwave56 The reason is, useState doesn't update the state counter immediately, it schedules it to be updated in next render cycle. Current function (addValue) is being evaluated in current cycle. When executing each statement it evalutes the expression and then executes. Like useState( 15 + 1) -> variable is replaced with actual value As counter is still not updated... every statement of useState(counter + 1) uses value 15 In useState(++counter ) scenario. This is preincrement. Equivalent code is Counter = counter + 1 UseState(counter) This say first increment the variable and then use it.
@PraveenKumar-2410 ай бұрын
Namaste Guruji... Great tutorial... completed the JS Series and now learing React... Thank you for your valuable contribution to the society..
@tajinder715 Жыл бұрын
Who is learning React for 1st time ? ❤
@NaturesCall5 Жыл бұрын
Meee
@kizigamer6895 Жыл бұрын
Me also but why you asked this
@RohitChhabirajYadav Жыл бұрын
@@kizigamer6895😂😂😂
@Rutvinayi Жыл бұрын
Hitesh sir please make js and react interview questions series....
@RohitSingh-je7fb Жыл бұрын
16 hoga print har jagah
@manasXDlol7 ай бұрын
Detailed explaination of why does first syntax only updates the count once: Initial State: Assume count is initially 69. First Call: setCount(count + 1) schedules a state update to set count to 70 (69 + 1). Second Call: setCount(count + 1) schedules another state update to set count to 70 (69 + 1), because count is still 69 in this scope. Third Call: setCount(count + 1) schedules yet another state update to set count to 70 (69 + 1), again because count is still 69 in this scope. In case of functional updater syntax React ensures that changes are made to the latest state of the count hence each function gets access to the latest state of the count variable: First Call: setCount(count =>count+1) schedules a state update to set count to 70 Second Call : schedules a state update to set count to (70+1) because count is now 70 in this scope and so on...
@awatanshsahay75706 ай бұрын
thanks bro for summarizing
@dineshshinde2116Ай бұрын
3:29 that will incrment only one value as the react took them in the form of the bundle and only update one value Grasp thing - setCouter (prevCounter => counter + 1) as the setCounter take call back and in that we can accesss the previous value of the counter and then we can update it according to the usage
@AsifButt-w1i Жыл бұрын
As a beginner in React, I am seeking every kind of knowledge regarding React. It was actually a nice video to be watched in order to get more deeper. Thanks HItesh!
@SatishK2022 Жыл бұрын
Value 16 hoga because setCounter ka jo last statement ha only wahi run hota ha ye or sab ko overwrite kar deta ha
@abhishekkashyap2984 Жыл бұрын
2:46 value of counter will only be incremented by 1 because react takes a snapshot of the state and render according.
@rishabhinc2936 Жыл бұрын
What do you mean by snapshot of the state ?
@abhishekkashyap2984 Жыл бұрын
@@rishabhinc2936 snapshot of state means , state at that particular moment
@anujkaushik_01Ай бұрын
There is also one question related to this which was also needed to be addressed. We provide a inital value to useState(0); say 0 but whenever state changes it again go for its initial value which may cause problem if this hardcoded values is coming from some sort of API or heavy computation, as it may overwhelm the APi or heavy computation process to call it on every state change
@visheshgupta49904 ай бұрын
itti acchi playlist banai hai itte acche se samjhaya hai to 200 comments target tha and it crossed 2000+ wohoo 🥳🥳 , Thanks for sharing this 🙌
@avinashrai436911 ай бұрын
Can i say that it also depends on how you are passing an argument to the setterMethod; 1. If you will pass simple variable to settermethod without callbackfunction it will refer that variable which you passed as argument, because we passed 4 times the same argument which is 15 that's why we got 16. 2. On the otherhand, when we use callback() function inside setterMethod() because the settermethod() returns a updatedvar which is accepted by callback is modified one that's why we are able to increment the variable 4 times. The callback is not refering the var is we passing instead it refering the var which is in the object of useState specific variable I mean counter, setCounter are a part of single object so the setCounter is refering the counter using this.counter that's the callback gets the returned var which is updated
@vivekhalder2974 Жыл бұрын
According to me, the answer should be 19 The actual answer...... So here the function calls will be sent in batches. So, react will see them as the same operation and perform it only once. So, the counter will increase by only 1 count. To increase it by four counts, use the callback which the setCounter accepts and increase it one by one. So, the first function will be called first, the callback will be executed and the next function call will be executed.
@ankitnayak372 Жыл бұрын
This is really a great video on learning about the async nature of the usestate function. And i always assumed that why do we need to use the previous value while updating the state. And this was the perfect example that taught me the difference when to use the callback func in updating the state. One thing is for sure, the way you teach is awesome and starting out concepts with the problem and solution approach is really superb!!! 👍
@sabitaliz7 Жыл бұрын
Kitne achi English hai bhai Tere 😢 main to confused ho jata hun ki kya comment karu 😂
@ankitnayak372 Жыл бұрын
@@sabitaliz7 Arre bro jaise maan kre waise bol do start toh kro bolna phir improve ho jaegi
@ab998234 ай бұрын
19, I was wrong. I saw this concept for the first time today. Now I will always remember this. Sir, please keep on making these type of videos, we love to learn the things that don't work as per our expectations, and ofcourse the explanation for it as well. Personally sir I love your style of teaching.
@kushaalrana21 күн бұрын
This is too good question. Thank you for this. Need more of these Interview Questions. Maza aagaya
@theGameofwebDevelopment Жыл бұрын
Everyone please comment on here. The reach is important for us to motivate sir to the highest and squeeze out the best number 1 content of React in hindi from Hitech sir.
@PrashantGupta-vz1pf6 ай бұрын
so the useState( )hook has a setCounter function name can be differ but it schedules jobs in batches so all the instructions inside setCounter will be sent once altogether so it sees that all are repeated instructions so its gets executed only once. But in case of call back function syntax inside setCounter it immediately increments the value and when the next time it excutes it agian increments like that then it doesnt works in batches.
@aksh1453 Жыл бұрын
16 hi hoga..because react uses batch updates...which also makes states feel like asynchronous function
@whatappstatus69373 ай бұрын
I've learned this concept before from the React docs and it's called state as a snapshot. When we write setCounter(counter+1) multiple times it is updated only once because in the onclick handler of that render, even after calling setCounter(counter + 1) multiple times the value of the counter is 15, because after our event handler finishes, then React re-renders the component. They explained very well in their docs but you also explained very beautifully sir ❤❤
@whatappstatus69373 ай бұрын
2:46 value of counter will only be incremented by 1 because react takes a snapshot of the state and render according.
@itssaifcode Жыл бұрын
I watched this type of question in a premium course and there it is told that react automatically takes only one of the set state, why will it take only one it was not mentioned, By watching your videos it feels like this course is better than other premium courses. Thank you so much for providing this type of content Sir.
@chaiaurcode Жыл бұрын
Ye channel bhi premium se kam nhi 😁
@itssaifcode Жыл бұрын
This channel is real premium, Thank you so much again sir for helping us this much.🙏🏻
@Bhushantbn Жыл бұрын
@@chaiaurcode premium channel nhi ye channel to Premium Pro...... he.....sir..🙏
@ThaCoders Жыл бұрын
@@chaiaurcode It may not happen that you also pay further after some time because collegeWallah's DSA series also did the same, taught some important things and then paid later. Sir, I feel bad to ask like this because I'll not afford paid course 😐 .
@bhagyashreenanda7049 Жыл бұрын
100% true@@chaiaurcode
@sonuviraaj3406 Жыл бұрын
Async function so it would be 16 only but if we want to log in console then it will update but not on ui....
@ankan-dev Жыл бұрын
It would be 16 only as the setState is an async call
@tajinder715 Жыл бұрын
hey Ankan
@ankan-dev Жыл бұрын
@@tajinder715 hi
@tajinder715 Жыл бұрын
@@ankan-dev i would like to connect with you on discord and want to take some suggestions there.
@ankan-dev Жыл бұрын
@@tajinder715 sure my username is ankan002
@Bobby-vs3zc2 ай бұрын
3:31 I'm making a prediction of 19. The reason is like how in js, if we increment for any number of times with i++ or i+1, it gets incremented.
@SKD14344AK4 ай бұрын
3:27 sir according to me , it will only increment the counter for one time not multiple times as you've duplicate it . The reason is that if it will increase the counter multiple times from default 15 so it'll become 19 , means after clicking on add button the counter will increment with 4. which is quite impossible . Because after increment it for one time the React re-render it and for that it'll create a New Virtual Dom and compare it from previous one , if the changes will same it'll not update the changes , but incase if there is actual changes , The React will update the changes .
@sayyedaamerasharali7583 Жыл бұрын
First of all THANKS for such an informative series. My question is from the previous video which is related to Tailwind and props. Other than Tailwind which UI library you will prefer like there is MUI etc, Bcz some of tailwinds feature are paid. Also I am Flutter developer so CSS / Bootstrap and other designing libraries me ‘ Meri expertise nahi hai 😄 Magar Mai is se OUT nahi hona chahta hun.’ Please help me regarding this with your valuable answer .
@AbhisekMaitiOfficial Жыл бұрын
tailwindui have subscription plan but the core classes are free in tailwind
@Freak_154 ай бұрын
3:29 Haan sir, mere according state 16 hogi then 17 hogi then 18 hogi then 19 hogi kyunki react fibre last wali value ko render karke layega toh value 19 honi chahiye as per mei, no cheating
@tanishqvishwakarma951825 күн бұрын
This course is way better than any paid course .
@saurabhbaij6 ай бұрын
I didn't understand this video in the first go.. But after reading a lot of comments and articles, now I have finally understood!
@muzzammilnoorkhan45367 ай бұрын
3:26 from when we click btn and the function call so it will not increment . Reason not store value due to number of repetition
@krishna.vineet6 ай бұрын
3:29 I guess 16, because pehle complete hoga ye pura, then dom bnega.. and ham counter++ nhi kr rhe, counter + 1, to har baar 15+1=16 hi hoga
@ImArslan-zq2mp2 ай бұрын
Very Interesting Concept Sir Bohot Helpful Tha Sir G Thanks For The Interview Question
@sumittodankar46317 ай бұрын
Thanks Hitesh.! You cleared all the concepts in-depth.
@bhansingh912 ай бұрын
sir apke sath learn bhi kr rhe h aur job interview ke bhi question pta chal ja rhe h thank you sir🙏🙏
@manasXDlol7 ай бұрын
I think the answer at 03:22 would be 19 , I think because Hitesh sir already explained us in Fiber architecture video, that react makes chunks of work and then render the UI rather than rendering it after every change.
@manasXDlol7 ай бұрын
I was wrong xd, anyways learnt something new!
@ImArslan-zq2mp2 ай бұрын
3:24 Sir Sachi Bta ra hu at that Moment Mujhe lg ra th ke 20 Print hoga But Kyunke mane Lecture suna hua ha Pehle to mujhe Pta ha ke sirf 1 Hi Incrment hoga or value 15 se 16 hojai gi
@kunalgarg49226 ай бұрын
3:05 Increment sirf 1 se honi chahiye, kyunki function har baar same counter hi ja raha hai. Shayad...
@vijaykumarb96227 ай бұрын
Sir, I am very happy with this course and your way of teaching. Just keep your magic ON.💗
@KrishnaKumar-kh5iu7 ай бұрын
Managing state updates in React components React components manage state updates efficiently by re-rendering only the necessary parts. Understanding how React manages state updates through reconciliation algorithms is essential. Optimizing state updates by batching them together improves performance and reduces unnecessary renders.
@choudhurymakes2 ай бұрын
Thanks for helping to improveing our interview knowledge
@HimilPrajapati5 ай бұрын
Wow, what a video, sir, I just learned this today, I've just started my day ( Day:4)
@aniketchile6465Ай бұрын
This series is best on youtube for react
@OnaijTimesАй бұрын
Sir literally me react ko koi itna mushkil bhala samajh raha tha but ap ke teaching way ne mujhe ghalat sabit sakrdya ❤
@commenfacts65834 ай бұрын
3:05 no because only one time we can declare vairable
@Manoj-eh7dn3 ай бұрын
Thank god I found these playlists
@RoronoaZoro78687Ай бұрын
16 hi print hoga kuki vo code override hojai ga 3:21
@_CodeCapsule6 ай бұрын
counter = counter +1 counter = counter +1 counter = counter +1 counter = counter +1 setCounter (counter) is se bhi same kaam ho rha hai
@PurePerfect4 ай бұрын
Bhai apka knowledge to Kamal ka h 😂
@truck-kun64832 ай бұрын
3:21 the value will be 16 but it will be updated 4 times
@mrgamerzyt39456 ай бұрын
3:25 idk why i think 16 will be printed since i think it is a trick question
@AshutoshPandey-bu7of2 ай бұрын
3:32 - I think sirt the value of counter will be 16 in UI
@RahulGupta-f9z5 ай бұрын
setCounter(counter +=1) setCounter(counter +=1) setCounter(counter +=1) setCounter(counter +=1) if i am doing this it will still gonna do the same thing instead of writing prevcounter
@Srijan8585 ай бұрын
yes same it is adding four times
@AkashYadav-di6kdКүн бұрын
3:13 My answer may be wrong, the value will increase by +4 on add value
@pro_gamer7843Ай бұрын
only 1 will increment and the value of counter will 16 the remaining incremental function will not work because react does not immediately change the state.
@AkashYadav-di6kdКүн бұрын
I want to share my insights. In actuall what happened, when the first line will be read setCounter(counter + 1) then closure comes into the picture and captures the current value of the counter i.e. 15 so finally setCounter( 15 + 1) => setCounter(16). Now, the useState schedules this for future. It is not sent to update immediately. Now, move to second line setCounter(counter + 1) here again closure captures the current value of counter i.e. 15 (because the last call has not sent yet ) so finally setCounter(15 + 1) => setCounter(16). Again useState schedule this for future. Same for 3rd and 4th call setCounter(15 + 1) => setCounter(16) Now, when the useState sent all these 4 calls setCounter(16) for the update then the final value will be 16 only. If you find any mistake then let me know in comments
@AmanWebDev7 күн бұрын
Today's learning done...👍👍
@swapnildapkosh28133 ай бұрын
initially we were not holding the previous value that's why we are not getting the desired answer, callback is saviour for such situations.
@MahakPandey-h2w7 ай бұрын
Sir ,want more such videos !! can you please create a playlist specially for interviews in js and react.
@gohilharsh501Ай бұрын
more interview questions please???
@kashishchaurasiya13626 ай бұрын
Hlw sir first of all tqsm for this amazing series and it is a oppertunity for me and other students to do something good by the way I think the answer of this interview questions is 19 according to me 😅 I know its wrong but still I tried
@rakeshbhetariya3 ай бұрын
once again grate lecture and if you can upload more vide like this one
@EngineerAnsh6 ай бұрын
3:00 kuch nhi hoga sir because we pass ( counter + 1) so aek line ho ya 100 line it will work like normal this is from my side don't see the answer now i see the answer
@sufiyanmogal15275 ай бұрын
The value will be 16 because states are asynchronous and not update the value in one render so all the counter state will not upate, if we want to get the current value in each setState function we can use arrow function but its not recommended becuase has some downgrades
@sparkygaming1746Ай бұрын
prev Value of useSate part of the video is very informative.
@hari43454 ай бұрын
at 7:41 this " function increment() { setCounter(++counter); setCounter(++counter); setCounter(++counter); setCounter(++counter); } " the same as " function increment() { setCounter((Counter)=>(Counter + 1)); setCounter((Counter)=>(Counter + 1)); setCounter((Counter)=>(Counter + 1)); setCounter((Counter)=>(Counter + 1)); } " what might me be the reason ?
@siddhanttripathi79434 ай бұрын
Initial response before knowing the answe r -> it will just increment by 1. not by 4.
@sarangkumarsingh790124 күн бұрын
Another exciting lecture....
@adityarajboum35726 ай бұрын
sirji aise mind kholne wale questions aaur chahiye
@mayanksoni16006 ай бұрын
3:20 -> Answer : 19
@muhammadawaisarshad45265 ай бұрын
i'm answering before watching video next, when we work with hook, when we call hook then our component re render it will not execute more then one time.
@hodophilesoul11713 күн бұрын
Thanku so much hitesh sir.
@HMArslan4 ай бұрын
3:24 I think only 1 update hoga but let's see sir kia hota ha ab ap hi btai
@hardikrawal49227 ай бұрын
3:24 answer-19 according to me
@sayanchakraborty21946 ай бұрын
Please Continue the series sir we need this
@Spsaab144 ай бұрын
By honesty... 3.40 sir 16 hi hoga...... Because jab setter function variable use krte h to uski default value hi add hoti h
@smartAi094 ай бұрын
hi hitesh sir , i am learn react your video is great
@nishantdholakia2244 ай бұрын
Answer should be 16 as we are not incrementing value of counter , we are just passing an int constant literal got after executing counter+1 expression
@kishan5036Ай бұрын
amazing teaching technique
@Tejas-gk5ze7 ай бұрын
sir 16 hi hoga, because setCounter aisa function nahi hai ki wo counter = counter + 1 kar rha hai, wo to counter + 1 ek new vairable mai store karke rerender kare pass kardeta hai counter ko jis se update ho jaate hai value hume lagta hai ki couter +1 hua hai jbaki asal mai ye new couter hai jiski value counter+1 hai.
@im9fg3 ай бұрын
16, tukka tha shi lga😅. anyway intresting concept discussed.
@dhorenihal34207 ай бұрын
Value will remain 16 as set is a call back will go to the task queue and get executed when the call stack becomes empty
@saptarsidebnath35162 ай бұрын
3:05 count value should be 15
@PoojaSharma-cd8qm6 ай бұрын
Very useful thank you sir❤
@debanshupati2075 ай бұрын
It will not increase like this as the value of the state depend on its previous value so I think we should do something to store the previous one and with taht the new one lets see at the end its my assumption now at the last will see. edit:now after seeing we can actually take the previous state data by call back as begginer I tried to ans the question and actually I do grab the basic logic thanks sir ,i will do better from future also
@gopitrip4 ай бұрын
interesting concept sir thank you
@sameershahidbuttaАй бұрын
3:14 mere hisaab se value 19 hogi
@SahilKhan-kh5uc5 ай бұрын
16 hi hoga because counter variable mein value update nahi ho rhi hai. isliye har line 15+1=16 hi hoga
@coderdas_114 ай бұрын
best series for react js.
@oneIIU5 ай бұрын
i think fiber should pause the immediate changes and entertain the latest one only, if that happens the value should increase by 1 only
@adityanagariya46456 ай бұрын
More Freshers's Interview Questions Sir
@PRINCESAHU-eb5ll6 ай бұрын
Thank you for this react series
@zindafossil383515 күн бұрын
How Intersting This is 👍
@--bountyhunter--6 ай бұрын
Underrated content creator
@sagar-tt4ub3 ай бұрын
very interesting sir where can I read more about such kind of topics where you mentioned that using a callback would update everytime, but passing directly would not
@sahinpathan51976 ай бұрын
Intresting question ✨
@abhaykashyap28483 ай бұрын
thanks sir for upload this type content
@ManojSingh-b1j3i4 ай бұрын
3:16 my honest Answer which is coming in my mind : that is ! 19