React Hooks Tutorial - 2 - useState Hook

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 27. 07. 2024
  • 📘 Courses - learn.codevolution.dev/
    💖 Support UPI - support.codevolution.dev/
    💖 Support PayPal - www.paypal.me/Codevolution
    đŸ’Ÿ Github - github.com/gopinav
    đŸ“± Follow Codevolution
    + Twitter - / codevolutionweb
    + Facebook - / codevolutionweb
    đŸ“« Business - codevolution.business@gmail.com
    useState Hook in React

Komentáƙe • 144

  • @tyrian_rets
    @tyrian_rets Pƙed 4 lety +43

    Thank you, for comparing the two ways of doing this - it is the only way inexperienced people (like myself) can genuinely get a grasp around these concepts.

  • @williamparrish2436
    @williamparrish2436 Pƙed 3 lety +6

    So fresh and so clean! So direct! I am 5 min in, and I know I will be doing this entire series. Thank you playa!

  • @sanjanasanikommu
    @sanjanasanikommu Pƙed 2 lety +4

    I am new to react and have development work in a week, this series is a booster dose !! Thanks for the amazing content.

  • @OliveME1119
    @OliveME1119 Pƙed 4 lety +4

    Thank you so much, I have read a lot online and watched a couple of tutorial videos as well but still couldn't fully well grasp what hooks are for and how to use this and that. This video is awesome and I understood (finally) what it is for. Thank you for guiding specially inexperienced people like me to be able to understand with clear explanations and comparison of the codes.

  • @arios0312
    @arios0312 Pƙed 4 lety +23

    You are one of the best online teachers around. Thank you so much!!

  • @siddisking
    @siddisking Pƙed 4 lety +3

    You have an extremely pleasing pace. Great video. Keep em coming :D

  • @akshaysrinivasan9363
    @akshaysrinivasan9363 Pƙed 4 lety +116

    Did Anyone realize how smoothly he asked us to subscribe and enable notifications in between.....

    • @lloyd_jvr9152
      @lloyd_jvr9152 Pƙed 4 lety +4

      That was amazing!

    • @KevinEontrainer381
      @KevinEontrainer381 Pƙed 4 lety +4

      Yeah I was just about to comment about that

    • @kisandrone
      @kisandrone Pƙed 4 lety +2

      Yes I noticed, Subscribe and Enable Notifications. Thats smart move.

    • @LetCode96666
      @LetCode96666 Pƙed 4 lety +1

      It was soooo smooth that I missed it first time but without re-watching the video I realized where he said that

    • @bostevens236
      @bostevens236 Pƙed 4 lety +1

      I exited the video and realized I had already liked and subscribed XD

  • @aimansaad6990
    @aimansaad6990 Pƙed 2 lety +1

    well explained! even for a newbie its easy to digest, disintegrating every thing in steps, telling the names, purpose and functions of terms, and giving a quick review of things once they are finished, your this video is a must watch for every beginner

  • @pr4veen_n
    @pr4veen_n Pƙed 4 lety +2

    Your Tutorials are excellent and very informative. In this example I was not getting the output in classCounter. I researched and found out that I need to bind the incrementCount method in the constructor Class. It would be helpful to update the newbies who don't know this yet.

  • @endthefed5304
    @endthefed5304 Pƙed rokem +2

    Being primarily a Java developer (Spring Boot, etc) I was leery of dismissing class components in favor of functional ones. Now I finally understand why this is a better approach. The various hooks, including useState, make this a much easier way to properly organize and configure components. thanks.

    • @mostinho7
      @mostinho7 Pƙed rokem +2

      I also come from Java but I don’t see the advantage of this approach. Would much rather have classes like a real programming language

  • @bostevens236
    @bostevens236 Pƙed 4 lety +1

    Excellent video! Thanks a lot for pushing me to go learn about array destructuring first!

  • @todaysmotivation9692
    @todaysmotivation9692 Pƙed 4 lety +1

    amazing explanation in such a clear and comparative way

  • @learningmaster8060
    @learningmaster8060 Pƙed 3 lety +1

    You explained very well, and the tutorial preparation is excellent. Keep the good work

  • @yo1414
    @yo1414 Pƙed 4 lety +1

    Love your videos - best on the market - and free! Thank you

  • @thomasnarkiss6319
    @thomasnarkiss6319 Pƙed 10 měsĂ­ci

    6:42-6:53 Oh I see what you did there. Considering the nonchalant tone it is ridiculously brilliant 😄

  • @amritanand4393
    @amritanand4393 Pƙed 4 lety +36

    "You can put random names to the variables, like Subscribe or EnableNotifications" Sly!

    • @s0ulweaver
      @s0ulweaver Pƙed 2 lety

      It was this moment when he knew he'd definitely win more subscribers than usual.

  • @amanbhadani8840
    @amanbhadani8840 Pƙed 2 lety

    Thanks a lot for explaining concepts like hook in much simpler way

  • @AfroKultur
    @AfroKultur Pƙed 3 lety

    The best Teacher out there!! Thank you.

  • @SergioArroyoSailing
    @SergioArroyoSailing Pƙed 3 lety

    great job starting off the series

  • @steveweiser1423
    @steveweiser1423 Pƙed 3 lety

    This is the best tutorial available!!!

  • @Nae_K-dramar
    @Nae_K-dramar Pƙed rokem

    This is the best React course so far

  • @vernitajones8189
    @vernitajones8189 Pƙed 4 lety +1

    You're so nice. Thank you for the simple explanation :D

  • @andesluu1411
    @andesluu1411 Pƙed 4 lety +8

    dang, i was kind of confusing on previous vids on destructuring; but after the use of it in this example, i will forever remember this concept =))))
    const [subscribes, enableNotification] = useState(0)

  • @robertg2285
    @robertg2285 Pƙed 5 lety +1

    your a great teacher, slow and very detailed

  • @joseZudaire
    @joseZudaire Pƙed rokem +1

    6:53 I see what you did there, you sneaky dog

  • @huanshao2165
    @huanshao2165 Pƙed 4 lety

    Super clear explanation! thanks!

  • @vamsikrishna-fp7wj
    @vamsikrishna-fp7wj Pƙed 5 lety +6

    It was an excellent tutorial on react js and Hooks .if you also make a tutorial on redux with react it can be lot more helpful.

  • @umeshkuralkar4085
    @umeshkuralkar4085 Pƙed 3 lety

    nice videos Vishwas, it is really great for beginners

  • @WhiteOwlStudios
    @WhiteOwlStudios Pƙed 4 lety

    thank you very much!!!!
    such a good tutorial!

  • @romitmohane4891
    @romitmohane4891 Pƙed 3 lety +1

    that segway 👀👀. Btw Awesome Vid!

  • @rasikapashankar9989
    @rasikapashankar9989 Pƙed 4 lety

    Very clear and understandable.thank you

  • @sergeykahnwald6640
    @sergeykahnwald6640 Pƙed 3 lety +1

    You are crazy man..
    me: *googles for angular material tutorial
    codevolution: hello
    me: *googles for react hooks
    codevolution: hi again
    me: *googles for some else random topic
    codevolution: yooooooooo hey
    Thank you ; )

  • @dedpossum66
    @dedpossum66 Pƙed 2 lety

    Thank you Vishvas!

  • @raunakkumar6144
    @raunakkumar6144 Pƙed rokem

    Best tutorial on react hooks.

  • @netsaosa4973
    @netsaosa4973 Pƙed rokem

    omg this is much more simpler than classes!!!

  • @AhamedKabeer-wn1jb
    @AhamedKabeer-wn1jb Pƙed 3 lety +1

    What a beautifull Explanation..

  • @yogsharma7807
    @yogsharma7807 Pƙed 4 lety +1

    Bhaiya ji aap to dev manus nikle :) :) :)

  • @this.channel
    @this.channel Pƙed 4 lety

    Very nicely explained.

  • @whysumancode
    @whysumancode Pƙed rokem

    I am enjoying it
    Thanks myannn

  • @justme364
    @justme364 Pƙed 2 lety +1

    Best teacher fr

  • @SnehasishGhoshSg
    @SnehasishGhoshSg Pƙed 5 lety

    Awesome job. am a Fan

  • @lalitsharma7482
    @lalitsharma7482 Pƙed 2 lety

    Big Fan bro. I really love the way you teach. Could you please tutorial video on react jest testing?

  • @chinnumanivannan
    @chinnumanivannan Pƙed 3 lety

    Excellent video

  • @chandrasekaran4611
    @chandrasekaran4611 Pƙed 2 lety

    super explanation

  • @melk48111
    @melk48111 Pƙed 4 lety +1

    Are you planning to start a series on React native?

  • @kamoliddintrade
    @kamoliddintrade Pƙed rokem

    Hooks learning started

  • @thecoderraj3777
    @thecoderraj3777 Pƙed 3 lety

    Thank you sir 🙂

  • @sns413
    @sns413 Pƙed 4 lety

    Kickass!

  • @kirankumarahir7933
    @kirankumarahir7933 Pƙed 5 lety +7

    You should make use of previous state in this.setState()

  • @riordan4750
    @riordan4750 Pƙed 2 lety

    how extension are you using for the shortcuts??
    I was trying to find JSX shortcut extensions but none of them had much shortcut

  • @21agdmnm
    @21agdmnm Pƙed 4 lety +2

    why exactly did we convert setCount(count + 1) into an arrow function? why ??

  • @aniketdeshpande9354
    @aniketdeshpande9354 Pƙed 3 lety

    Hi Vishwas,
    Please make tutorial on testing react app using jest and enzyme.....it would be very helpful for new learners like me

  • @RakeshGauniyal
    @RakeshGauniyal Pƙed 4 lety

    Play speed = 1.25 ✅
    Awesome and Informative video though.

  • @OvRaf
    @OvRaf Pƙed 4 lety

    thanks!

  • @mahesh5452
    @mahesh5452 Pƙed rokem

    Thanks

  • @mr.techno-era7474
    @mr.techno-era7474 Pƙed 5 lety +9

    Previous state?? How do u increment counter without it?

    • @flashfs
      @flashfs Pƙed 4 lety +1

      It would be safe to use previous state, but for the sake of this example, it works using this.state, because by the time you click the button again, this.state already has a new value. This is my opinion.

  • @AhamedKabeer-wn1jb
    @AhamedKabeer-wn1jb Pƙed 4 lety

    Thank you..

  • @abdullahimran4262
    @abdullahimran4262 Pƙed 3 lety

    thank you

  • @yashparekh2954
    @yashparekh2954 Pƙed 4 lety +3

    Hi @Codevolution,
    Thanks for excellent tutorial but at 10:23 when you mentioned rules, you told not to use hooks inside the condition, nested components and loops would you clarify that portion a bit. Say there is a case for the network request and we are fetching data using anxious inside the then and then changing something on the state, what to do then?
    Thanks in advance.

    • @ashishrai5720
      @ashishrai5720 Pƙed rokem

      did you get the answer? its been two years since you have had this doubt.

    • @anywho3934
      @anywho3934 Pƙed rokem

      @@ashishrai5720 did you ?

  • @arunkumar-mm7qs
    @arunkumar-mm7qs Pƙed 2 lety

    Awesome

  • @MrCuteguylol
    @MrCuteguylol Pƙed 3 lety

    He sounds kind of like Bartemius Crouch Senior from harry potter :O

  • @adarshsasidharan254
    @adarshsasidharan254 Pƙed rokem

    in the class component, why didn't you use the callback function which receives previousState as argument?

  • @questreal5812
    @questreal5812 Pƙed 3 lety

    Since we are using the previous state to increment the counter shouldn't we setState like this
    this.setState(prevState=>({
    count: prevState.count+1
    }))

  • @yangzhihong1509
    @yangzhihong1509 Pƙed 4 lety

    Very clear explanation, just one thing don't quite understand. Why define count as const but instead it actually changes?

  • @SedhuRaguraman
    @SedhuRaguraman Pƙed 3 lety

    Are you have any video tutorials for React boilerplate setup with hooks? if you have pls add the link

  • @adityabilgundi
    @adityabilgundi Pƙed 2 lety

    here array destructuring is different from object destructuring as the useState( ) returns a pair

  • @userozancinci
    @userozancinci Pƙed 2 lety

    is it possible to use onClick={()=>this.setState({count:this.state.count+1})} in class component???

  • @vigneshgvs
    @vigneshgvs Pƙed 2 lety

    class way: class component, state variable, setState in an arrow function
    hooks way: func component, useState with array restructuring - variable & method, call that method from arrow function

  • @hiranmayeemishra4420
    @hiranmayeemishra4420 Pƙed 2 lety

    need a playlist in functional components please!

  • @chintspanchal
    @chintspanchal Pƙed 3 lety

    Rules for Hooks: What is the difference between React functional component and regular JS function??

  • @SepSol75
    @SepSol75 Pƙed 4 lety +2

    7:32 I didn't quite get this part. Why did you put setCount() method in an arrow function? What is a 'function call' and why does it specifically need an arrow function?

    • @storiesfromincredibleindia
      @storiesfromincredibleindia Pƙed 4 lety +3

      Hi, that was done because if arrow function was not used, the function would be called directly on page load. In order to avoid that default call, he is using the arrow function. I hope it clarifies.

    • @SepSol75
      @SepSol75 Pƙed 4 lety +1

      @@storiesfromincredibleindia That did clarify indeed, thanks :)

    • @laughtale1181
      @laughtale1181 Pƙed 2 lety

      @@storiesfromincredibleindia thanks i too always had this doubt

  • @RishabhJain-uv7zj
    @RishabhJain-uv7zj Pƙed 3 lety +1

    why did he convert the function calls to arrow functions. Can't it be just the functional call?

  • @paraschawla3757
    @paraschawla3757 Pƙed 2 lety

    when to use inline function in onClick setCount(count+1)}>
    vs
    just a function call like

  • @veermetri05
    @veermetri05 Pƙed 4 lety +1

    6:42 nice examples subscribe and enable notification

  • @toannew
    @toannew Pƙed 3 lety

    Please help, I don't really understand why we need to use arrow function at 7:34 ?

  • @murtazako
    @murtazako Pƙed 5 lety +5

    Have a question. Why prevState is not used in this example?

    • @mr.techno-era7474
      @mr.techno-era7474 Pƙed 5 lety

      Yeah... How can he increment value??

    • @KrishnavsVenus
      @KrishnavsVenus Pƙed 5 lety

      because we only increment it by 1 for 1 click and not by 5 in 1 click?

    • @guilhermediniz2809
      @guilhermediniz2809 Pƙed 4 lety

      Yeah, I didnt get that either

    • @guilhermediniz2809
      @guilhermediniz2809 Pƙed 4 lety +1

      But it seems to me that we can use both ways, although the prevState method is more reliable

  • @hamzasabri5610
    @hamzasabri5610 Pƙed 3 lety

    first of all amazing explanation of the hocks thank you very much
    second thing what is the name of the snippet? I don't have the "rfce" in the one that I have

    • @manishthomas341
      @manishthomas341 Pƙed 3 lety +5

      Add 'ES7 React/Redux/GraphQL/React-Native snippets' in VS Code

    • @shanemotyl
      @shanemotyl Pƙed 3 lety +1

      So glad it's not just me

  • @abhishek_k7
    @abhishek_k7 Pƙed 4 lety

    1000th like! Yay!

  • @thatprasadguy
    @thatprasadguy Pƙed 12 dny

    Anyone, why did we passs the setCount function on arrow function? onClick={() => setCount(count + 1)}

  • @RamaKrishnaMallampally
    @RamaKrishnaMallampally Pƙed 4 lety

    Please mention diff b/w React functions and Regular Javascript functions

    • @vihan9488
      @vihan9488 Pƙed 2 lety

      React functions are regular JavaScript functions that takes object of props and return jsx(HTML)

  • @liumuguan6945
    @liumuguan6945 Pƙed 3 lety

    One question: since count is set as const, how can it be changed by every click? Thanks

    • @simple8810
      @simple8810 Pƙed 3 lety +2

      const cannot change through re-assignment
      const cannot be re-declared
      but on click we just adding new value to that means to array we are not changing the whole array ,
      like const count=[1] ,and const count =[2] gives error
      but count.push(2) will not give error

  • @jeromeabril5001
    @jeromeabril5001 Pƙed 3 lety

    Can someone give the link to the snippet that he is using?

  • @divyanshubist8527
    @divyanshubist8527 Pƙed 4 lety +1

    Can someone explain why he didn't bind the event handling while creating class component??thanks in advance

    • @orafss
      @orafss Pƙed 4 lety

      Arrow function function dont need binding

    • @NyKunPikaDasGalaxias
      @NyKunPikaDasGalaxias Pƙed 4 lety

      It's a shortcut that Babel let us do. You can just do that instead of manually binding.

    • @rutvikjobanputra2968
      @rutvikjobanputra2968 Pƙed 3 lety

      Arrow function is one way of binding
      He uses increment count function as arrow function
      So i hope now u get it

  • @priyanshuchaurasiya6184
    @priyanshuchaurasiya6184 Pƙed 6 měsĂ­ci

    The Moment : You can use any variable name in array destructuring like subscribe and enableNotification 😅😅

  • @petrsuchy6386
    @petrsuchy6386 Pƙed 7 měsĂ­ci

    You said before that whenever you have to update state based on the previous one we need to pass a function as argument to setState
    instead of passing in an object. So why you don't stick to that in the class component example? 😖

    • @petrsuchy6386
      @petrsuchy6386 Pƙed 7 měsĂ­ci

      In the third part of this series it is fixed and well explained :)

  • @ujjvalsharma5055
    @ujjvalsharma5055 Pƙed 3 lety

    I have a small question
    {count} is incorrect while
    setCount(count+1)}>{count} is correct
    Why do we need arrow function?

    • @milkmyshake
      @milkmyshake Pƙed rokem

      You need to review "ReactJS Tutorial - 13 - Event Handling"

  • @pavanibattina9837
    @pavanibattina9837 Pƙed 2 lety

    Why we are giving arrow function in onclick

  • @impossibleexperience
    @impossibleexperience Pƙed 4 lety

    what IDE are you using is this tutorial?

  • @shashankpandey1966
    @shashankpandey1966 Pƙed 3 lety

    increment count is not defined ??

  • @allpal3077
    @allpal3077 Pƙed 3 lety

    How to persist state on refresh?

  • @thisiskyle3248
    @thisiskyle3248 Pƙed 4 lety +1

    Why your App.js component has no render()?

    • @andreiosypchuck5081
      @andreiosypchuck5081 Pƙed 4 lety +1

      because App.js component is a functional component, not a class component

    • @thisiskyle3248
      @thisiskyle3248 Pƙed 4 lety

      @@andreiosypchuck5081 Okay thanks! noted. I just discovered this a while ago. Good luck

  • @chetanpl
    @chetanpl Pƙed 3 lety

    Can any one tell me , can i use 'useState' in class. Because i try to use it underclass and it is giving me error.

    • @codersdream9013
      @codersdream9013 Pƙed 3 lety

      UseState is for stateless (function) components. For class components, use setState instead. You can watch his playlist previous videos or Google it...

  • @priyanshubhardwaj2158
    @priyanshubhardwaj2158 Pƙed 3 lety +1

    I have two questions (Class-based component).
    1) How did it work without binding the method in Class component?
    2) Without keeping the prevState how is this thing incrementing the count value?
    Anyone who knows these answer would be appreciated. Thanks!

    • @simple8810
      @simple8810 Pƙed 3 lety +1

      when using arrow function then no need of binding this with method used
      and using previous state is good way and u will need to use that but here simple count+1 also work but previous state is not stored
      like using count+1 again count+1 and many times it will be incremented just by one

    • @nadicadizdarevic2835
      @nadicadizdarevic2835 Pƙed 2 lety

      @@simple8810 I also wanted to ask for a binding inside of a constructor...Thanks...Save me a lot of time.. Is it always a rule ? Thanks once again.

  • @sachinbabar4567
    @sachinbabar4567 Pƙed 4 lety

    Any paid service for react js?

  • @mohithsaisripothineni6583

    count is declared as constant in line 5 , how can it be mutated ?

    • @mohithsaisripothineni6583
      @mohithsaisripothineni6583 Pƙed 3 lety

      useState(0) returns an array the const is a reference to array but values inside the array can be changed. in line 5 instead of taking it as an array the tutor directly destructured the array

    • @O1L2E3G
      @O1L2E3G Pƙed 3 lety

      SetCount function do not mutate LOCAL counter variable.
      It changes "state" of Func Comp (React core).
      Each time state changes - React call Func Component again, and rerender Component setting a new state VALUE to the NEW local counter variable.
      imho.

  • @nmanikiran
    @nmanikiran Pƙed 4 lety

    why they used Array destructuring, why not object destructuring?

    • @flashfs
      @flashfs Pƙed 4 lety

      Because React.useState() returns an array and not an object.

    • @nmanikiran
      @nmanikiran Pƙed 4 lety

      @@flashfs my question is why Facebook react.js team used array destructuring ?

    • @flashfs
      @flashfs Pƙed 4 lety +1

      That I don't know... I think they could have done like an object if they want to. Maybe they thought an array would keep value and setValue more organized. But I really don't know.

  • @dantewhite7659
    @dantewhite7659 Pƙed 4 lety

    :)

  • @toannew
    @toannew Pƙed 3 lety

    3:15 code for the class state
    10:23 Rules of hook

  • @jenasusil6
    @jenasusil6 Pƙed 3 lety

    RSC

  • @Pareshbpatel
    @Pareshbpatel Pƙed rokem +1

    {2022-10-14}