React Hooks useState Tutorial

Поделиться
HTML-код
  • Опубликовано: 3 окт 2024

Комментарии • 136

  • @Ko0lHaNDLuKex
    @Ko0lHaNDLuKex 5 лет назад +70

    You deserve more subscribers, Ben! You teach really well. Learnt a few things despite initially thinking this was just for beginners Hooks intro!

  • @channel23653
    @channel23653 3 года назад +5

    Thank you for sharing this! takes me 1 hour to understand the custom form hook, how you pass the initial empty object into the final return and what is synthetic event, now seems easy. Totally for this 15 minutes tutorial I spent 4 hours, including a better understanding for the destructuring, spread and ..rest.

  • @Justin_Min_001
    @Justin_Min_001 4 года назад +8

    I feel so upgraded after watching this tutorial. Thank you!

  • @bantinggamer4962
    @bantinggamer4962 4 года назад +12

    i should have totally given this a watch ages ago. thanks Ben your the best!

  • @TubeSwarn
    @TubeSwarn 4 года назад

    Much more information in this 15 mins of video than any other useState tutorials in RUclips

  • @sathishkumarr7106
    @sathishkumarr7106 3 года назад +1

    You are typing so fast 🤩🥳🥳 and teaching so well. I want to be react developer like you🤩

  • @DevlogBill
    @DevlogBill 2 года назад

    Wow Ben, seen a couple of tutorials on React fairly recently but never seen not one use React the way you had in this video. I was really impressed. Just started React 4 days ago and I have lots to learn. I saved your video, this is a really good video to refer too, thanks for creating it.

  • @GL-fc2pv
    @GL-fc2pv 5 лет назад +6

    the best explanations on youtube

  • @zaretix
    @zaretix 4 года назад +5

    I feel like this is peak React/JavaScript. It's so intuitive and clean

  • @mostinho7
    @mostinho7 11 месяцев назад +1

    The useState hook returns two things in an array the state variable and a setter. The setter takes the old value for you to update it
    6:50 if using object as state in useState you need to use the spread operator to get the rest of the object properties, if you just return the property you want to update then you’ll erase the other properties the object has

  • @imanmohamadi2827
    @imanmohamadi2827 5 лет назад

    I like the way you teach and talk about the stuff that I expect but is not usually used (like the array definition instead of array destructuring). This helps me connect much more effectively to the content and frees up my mind from having any questions hanging in my mind until the end of the video.

  • @SomeGamerDudeGuy
    @SomeGamerDudeGuy 5 лет назад +1

    Please continue this series really enjoy learning from you.

  • @stoic2454
    @stoic2454 3 года назад +1

    Is there anyone who's like if I see this - "do you wanna be a software engineer..." thing one more time, I'll crack my screen?
    PS: Gr8 vdo ben as always

  • @meganadams7274
    @meganadams7274 4 года назад +1

    Best tutorial I've watched in a long time. I'm gonna guess you're a professional teacher irl haha

    • @bawad
      @bawad  4 года назад +1

      actually RUclips is the only place I teach, but I've made a lot of videos to practice

  • @func0der
    @func0der 5 лет назад +5

    Clear explanation of React useState hook. Thank you very much :)

  • @armanrozika
    @armanrozika 4 года назад +7

    Man, you're so fast when explaining, I have to pause the video and then think lol. Anyway great content, thanks!

  • @Meskarts
    @Meskarts 4 года назад

    Thanks alot Dude! Was searching for this .. very nice spoken - explained! Please do more!

    • @bawad
      @bawad  4 года назад +1

      thanks, this is the rest of the series ruclips.net/p/PLN3n1USn4xlmyw3ebYuZmGp60mcENitdM

  • @21agdmnm
    @21agdmnm 4 года назад +1

    so glad I found your channel! amazing tutorials! Subscribed !

    • @bawad
      @bawad  4 года назад

      welcome :)

  • @pianoby40
    @pianoby40 4 года назад

    Thanks Ben. These series really helps me a lot of time understanding react mind 👍👍👍

  • @ansonthedev
    @ansonthedev 4 года назад

    Jesus, you make me want to learn VIM just watching you go with the keybindings.

    • @bawad
      @bawad  4 года назад +2

      Do it benawad.com/vim

  • @mudandmoss4132
    @mudandmoss4132 4 года назад

    Bro your videos are sick, subscribed.

  • @NyKunPikaDasGalaxias
    @NyKunPikaDasGalaxias 4 года назад +1

    Ben, you're amazing! Thank you very much.

  • @jlmonroy13
    @jlmonroy13 4 года назад

    what a clear explanation man! Thank you!

  • @juancamiloq1
    @juancamiloq1 5 лет назад +2

    Awesome series

  • @leonardocamargo5412
    @leonardocamargo5412 4 года назад +1

    Thank you, for this playlist about React Hooks. Is Amazing.

  • @mr.RAND5584
    @mr.RAND5584 4 года назад

    Thank you so much for sharing your knowledge. Amazing Tutorials.

  • @mfaraz07448
    @mfaraz07448 4 года назад

    Hi Ben,
    you're such a lifesaver man you explain well how to change the state immediately
    setFindDeals(currentState => ({ SubCategoryID: currentState.SubCategoryID = option.key }))
    Thanks Man....!

  • @KbIPbIL0
    @KbIPbIL0 4 года назад

    man your tutorials are amazing

    • @bawad
      @bawad  4 года назад

      thanks :)

  • @ibrahimalshubaily9520
    @ibrahimalshubaily9520 Год назад

    Great! Thx Ben.

  • @markod3957
    @markod3957 4 года назад

    Wow dude, your content is fantastic!

  • @gosnooky
    @gosnooky 5 лет назад +1

    I've always been a huge proponent of OOP, and I still am in most cases, but you can't argue the efficiency of hooks.... I can condense a huge, complicated form into one succinct metho... err, function.

  • @catrosits
    @catrosits 4 года назад

    Thanks.. your manner of explaining is awesome.. keep it up1

  • @harshwardhanjethania
    @harshwardhanjethania 4 года назад

    I like your videos and the way you explain the things, I would like you to create a video explaining how to get the updated state values just after setting them, as callback is not allowed in useState hook.

  • @HeroesOfAesthetics
    @HeroesOfAesthetics 4 года назад

    Great video man!! I learned so much!! One random question, do you actually type that fast or is it fast forwarded lol

    • @bawad
      @bawad  4 года назад +3

      benawad.com/fast

  • @tkousek1
    @tkousek1 3 года назад

    This is great. Thank you so much.

  • @alwinvillero4404
    @alwinvillero4404 3 года назад +6

    How do you code so freaking fast, almighty Ben?

  • @iamkiko5046
    @iamkiko5046 5 лет назад +1

    You rock man, thank you.

  • @svetozarradic9667
    @svetozarradic9667 3 года назад +1

    Hey Ben, thank you for this wonderful hooks tutorial. Do you know maybe how to write tests for the components which use hooks?

  • @rameshthati7999
    @rameshthati7999 3 года назад

    Hi Ben,
    I like way of your presentation. I have one small request, if it possible can you please share some session about react testing

  • @CrusaderMen
    @CrusaderMen 4 года назад

    Thank you Ben!

  • @christianangelomsulit3759
    @christianangelomsulit3759 4 года назад

    Love this channel !

  • @abrahamgarcia2171
    @abrahamgarcia2171 4 года назад

    About the lint errors at the beginning of the video, is an internal configuration that came when you create a react app with the create-react-app module. It also includes lint errors for useEffect hooks

  • @yehoshualeventhal5078
    @yehoshualeventhal5078 4 года назад +1

    hey great stuff ben.
    If anyone would like to know how to clear the form with the useForm hook, you can just return a 3rd function with the hook that looks like this
    () => {
    setValues( {
    ...initialState
    })
    }

    • @Frazful
      @Frazful 2 года назад

      Hey! This sounds like a great idea, how would you call it though?

    • @Frazful
      @Frazful 2 года назад +1

      Nvm, I figured it out. Thanks!

  • @nirmalpatel29
    @nirmalpatel29 4 года назад

    Hello, I have one doubt. Why the count takes two clicks to increment a count. Here's the onClick event. setCount(count++)}>+. I know I did " count ++ " If the initial count is 10, when I click on the + button it should increase the value of count. But the state doesn't change on the first click. Why so?

  • @abbisalmi1948
    @abbisalmi1948 3 года назад

    Thank you for this

  • @danieljing9319
    @danieljing9319 4 года назад +2

    Why use [ ] to close [e.target.name]

    • @bawad
      @bawad  4 года назад +4

      const name = "firstName"
      { [name]: "ben" }
      is equivalent to
      { "firstName": "ben" }

    • @danieljing9319
      @danieljing9319 4 года назад

      @@bawad thanks, that's ES6 syntax

  • @mwnkt
    @mwnkt Год назад

    just wondering if you ever use formik with the useForm hook. or maybe do you prefer wrapping your forms with the formik component

  • @filteredproducts
    @filteredproducts Год назад

    Awesome

  • @khamhti5223
    @khamhti5223 Год назад

    Thanks

  • @jamols09
    @jamols09 3 года назад

    is useState really necessary for handling inputs like username, password, email ? What's the most preferred way? I just want to keep it simple not like this one.

  • @olegnamedia2355
    @olegnamedia2355 4 года назад

    Why is e.target.name enclosed between square brackets and not just single or double quotes?

  • @saadbaig3060
    @saadbaig3060 4 года назад

    you got my sub

  • @thanatos454
    @thanatos454 5 лет назад +2

    The playlist link in the description is to your Mini Google Docs Clone series.

  • @vvchernyshenko
    @vvchernyshenko 4 года назад

    Absence of setState callback function in useState hook is the biggest fail. It helped me so many times.

  • @ekbergpeter6987
    @ekbergpeter6987 3 года назад

    Just another one who puts up tons of good material, but unfortunately to busy to answer any questions.

  • @DevlogBill
    @DevlogBill Год назад

    Hi Ben, I'm a super newbie. Question? Why were classes replaced with hooks? I am enjoying hooks and classes look more complex. But I am pretty sure there is another reason why classes were replaced for hooks, why were hooks introduced and classes replaced?

  • @mandiprai8593
    @mandiprai8593 4 года назад

    Since the introduction of hooks, is it better to just use functional components compared to the class components?

  • @robertshawnmitchell
    @robertshawnmitchell 5 лет назад +1

    Thought you were all in on Typescript

    • @bawad
      @bawad  5 лет назад +2

      I am, but for some beginner stuff, I want to use javascript so it's more accessible

  • @trueMatthewkirkham
    @trueMatthewkirkham 3 года назад

    are you aware that web-dev-simplified basically just copied this video? I preferred yours though. Love your shit man.

  • @nicken86
    @nicken86 5 лет назад +2

    Thanks Ben! How would you handle the custom state hook in typescript?

    • @bawad
      @bawad  5 лет назад +1

      I would probably do: gist.github.com/benawad/93f1734ba5771ea53615be6f6fd89721

    • @larsap
      @larsap 4 года назад

      @@bawad Thanks for the help on moving it over to typescript, but I'm struggling to implement it. I'm still learning typescript and can't seem to type the object properties that are returned to a string or number. Anyway you can show how to use the function?
      Error with example pass of property firstName: Property 'firstName' does not exist on type '(e: any) => void'

    • @bawad
      @bawad  4 года назад

      feel free to post the code in my discord, and I'll take a look at it discord.gg/Vehs99V

  • @dayashankar1431
    @dayashankar1431 5 лет назад

    Hi Ben. Thanks for the video. I had a question:
    Why did you use setCount(c=>c+1) instead of setCount(count+1)? As each variable has its own updater function, can you give an example of when the state override issue (which used to happen with setState) might happen when using useState?

    • @bawad
      @bawad  5 лет назад +4

      try
      setCount(c=>c+1)
      setCount(c=>c+1)
      vs
      setCount(count+1)
      setCount(count+1)

  • @nguyenchau7107
    @nguyenchau7107 3 года назад

    Damn I wish I know this channel earlier

  • @yangwang24
    @yangwang24 4 года назад

    Hi Ben, thanks for your video. Just one quick question here: If I put "const [count, setCount] = React.useState(expensiveInitialState);" which is also working. May i know which one is correct?

  • @kukuster
    @kukuster 3 года назад

    How do you make something like "delete everything you the right up to the first matching closing parenthesis"?
    What's the vscode action for this?
    E.g. starting from 7:55

  • @abhim6380
    @abhim6380 4 года назад

    The "values" (values.email etc) changes every time state changes though you're assigning only once when calling useForm. How? useForm is a simple function that returns an array. Does values get updated every time you call setValue. Is the useForm function called again with new values set to initialValues or something like that? It's so confusing

  • @danianiazi8229
    @danianiazi8229 3 года назад

    'useState' is not defined anyone please?

  • @gevikb
    @gevikb 4 года назад

    Could you please explain a bit more about why the usage this useStare (useForm) is preferred over using Formik? Thank you :)

    • @bawad
      @bawad  4 года назад +2

      I prefer to use formik, just wanted to show how you could build your own

    • @gevikb
      @gevikb 4 года назад

      @@bawad Thank you.

  • @abhim6380
    @abhim6380 4 года назад

    I don't understand why app.js is re rendered when the state changes in the useform file. You are simply returning an object in useform

    • @CynthiaSotoCaballerolYEAHl
      @CynthiaSotoCaballerolYEAHl 3 года назад

      Yes, but you are using useState in there

    • @abhim6380
      @abhim6380 2 года назад

      @@CynthiaSotoCaballerolYEAHl OK, I was under the impression that hooks can be created only inside components.

  • @jessekrim
    @jessekrim 3 года назад

    How would you do this in React Native? What is the rn alternative for e.target.value ? Thank you!!

  • @SahraClayton
    @SahraClayton 4 года назад +1

    Bro you type super fast and move around vs code fast, how do you do that

    • @bawad
      @bawad  4 года назад

      www.benawad.com/faq/#how-do-you-code-so-fast

  • @barryfulk6820
    @barryfulk6820 5 лет назад

    I'm pretty sure Ben is a 10x developer ;)

  • @pepepomada4664
    @pepepomada4664 5 лет назад

    How you can access the event (e) inside handleChange hook if you didn't pass it with a callback like so:
    (e) => handleChange(e)
    How this work and how that javascript (or general programming) feature is called so I can search for it?
    By the way awesome tutorial!!

    • @bawad
      @bawad  5 лет назад +1

      That's an arrow function, it's like creating a callback on the fly

    • @pepepomada4664
      @pepepomada4664 5 лет назад

      @@bawad Yes, I understand that. But in the video at 12:50 you return a function as the second value of an array from useForm hook . That function takes a parameter "e" (the event from onClick):
      e => {
      setValues({
      ...values,
      [e.target.name]: e.target.value
      }
      );
      }
      The problem is that you never pass the argument "e" as a parameter. At 13:14 you change the arrow function that does pass the a parameter "e" for just the name of the function "handleChange" without parameters.

    • @pepepomada4664
      @pepepomada4664 5 лет назад

      @@bawad Ok forget it. It is because I thought that onclick have to be passed a function with parenthesis or a arrow function but now reading the documentation clearly shows that you can pass the name of a function or a function expression like in this case.

  • @mongonode6755
    @mongonode6755 3 года назад

    Can anybody please explain me the return statement at 13:33?

  • @jacksmith870
    @jacksmith870 5 лет назад

    5:05 I didn't get the side note. Any detailed explaination for it ben? Thanks.

    • @bawad
      @bawad  5 лет назад +1

      try doing
      setCount(count + 1)
      setCount(count + 1)
      then try
      setCount(c => c + 1)
      setCount(c => c + 1)

  • @dimitarvel1441
    @dimitarvel1441 5 лет назад

    This is why I dropped angular. When they changed the entire fucking way of writing angular (years ago) and even removed backwards compatibility I was so pissed that I wrote a 3 pages complaint and sent it to their dev team members.
    Now the same thing is happening in react. Why do they have to do this? If it works, let it be. Not like we are seeing some major improvements in performance, it's stupid.
    The thing is that we have to think about react in a new way now. And I just learnt the framework about 8 months ago...
    The thing is, developers don't want frameworks to be easy, they want them to be efficient and consistent.
    If they kill backwards compatible in going to quit web development for good. It's been too fucking long trying to keep up because companies can't accept the fact that they can't make a framework that is easy, fast and efficient. If any collage student with half a month of experience in javascipt is able to pick up something like angular, then where does my fucking 6 years experience go? Down the fucking drain, that's where it fucking goes.
    And not to mention that the documentation on the react website is garbage so I have to watch RUclips tutorials.

    • @bawad
      @bawad  5 лет назад

      I feel ya

  • @sushilbansalk
    @sushilbansalk 5 лет назад

    Hi Ben, do you have example of useState with Formik using TS? i am using Formik in my project and was thinking of refactoring the code.

    • @bawad
      @bawad  5 лет назад

      Why are you wanting use them together? Usually I store the form state all of in formik

    • @sushilbansalk
      @sushilbansalk 5 лет назад

      @@bawad I do the same. I thought i will need to use useState to work with Formik. Apologies for wrong assumption there.

  • @newrind
    @newrind 5 лет назад

    Ben are you using brown switches?

  • @salemouail627
    @salemouail627 4 года назад

    on 7:14 how did u delete that comma without moving cursor ?

    • @bawad
      @bawad  4 года назад +1

      vim benawad.com/vim

    • @salemouail627
      @salemouail627 4 года назад

      @@bawad thanks so much
      And thanks for the turorial ♡

  • @johnnwabuforudemezue1108
    @johnnwabuforudemezue1108 3 года назад

    You made this very simple stuff a complete sham,God help you

  • @rajdeepchandra9807
    @rajdeepchandra9807 5 лет назад

    If i want to use this hook for react native text input with onChangeText i get e.target as undefined. any idea on how can i achieve this with React Native?

    • @bawad
      @bawad  5 лет назад +1

      onChangeText just gives you the text, so:
      (name, text) => setValues({...values, [name]: text })

  • @AmanNidhi
    @AmanNidhi 5 лет назад

    is this part of a playlist ?

    • @bawad
      @bawad  5 лет назад

      It's the start of one: ruclips.net/p/PLN3n1USn4xlmyw3ebYuZmGp60mcENitdM

  • @toponthelist9154
    @toponthelist9154 4 года назад

    Do you use Vim inside VS Code?

    • @bawad
      @bawad  4 года назад

      yeah

  • @5ervanttechintel_github_io979
    @5ervanttechintel_github_io979 5 лет назад +2

    *So sad, you can't use TypeScript with React Hooks...*

  • @keepforever726
    @keepforever726 5 лет назад

    How would you go about clearing the fields after, say, a submit?
    I accomplished it by returning an additional function in the useForm array that I pull out as "clearForm": github.com/keepforever/use-ctx-mem-rdcr-6-22-19/blob/5e4863dd6f530737ad807d3ef81e581df8d9082f/src/hooks/useFormQuestion.js#L15
    Or would you recommend another strategy?
    Also, what about yet another method on the custom hook that fetches (async) data? Bad practice or fair game for the right situation?
    github.com/keepforever/use-ctx-mem-rdcr-6-22-19/blob/5e4863dd6f530737ad807d3ef81e581df8d9082f/src/hooks/useFormQuestion.js#L27

    • @bawad
      @bawad  5 лет назад

      I would probably do gist.github.com/benawad/3d95a0acd4be329afb05cc570ccd545c

  • @jexxiewoo8390
    @jexxiewoo8390 5 лет назад

    Could you explain more why "Function components capture the rendered values. With Hooks, the same principle applies to state as well." in this bit.ly/2Hh4qQw? Thanks!

    • @bawad
      @bawad  5 лет назад

      basically the functions snapshot the values of the variables when rendered

  • @davidfromnorth7836
    @davidfromnorth7836 4 года назад

    10:59 oops

  • @necbottz
    @necbottz 4 года назад

    i fail to set custom hook as explained. i fall exact what your teaching and i real understand but i get this error
    TypeError: Invalid attempt to destructure non-iterable instance

    • @bawad
      @bawad  4 года назад +1

      Have you tried cloning my repo?

    • @necbottz
      @necbottz 4 года назад

      no i didnt clone i was just folloeing maybe let me try that. By the way thanks for reply

  • @alexsilny5748
    @alexsilny5748 4 года назад +1

    Great video tutorial. Thanks. It helped me get an understanding of Hooks. I do have a question? In ruclips.net/video/9xhKH43llhU/видео.html you are returning an array in the useForm Hook. Can you also give an example of how you would return an object?

    • @bawad
      @bawad  4 года назад +2

      return {
      values,
      handleChange: e => {}
      }