React Hooks useState Tutorial

  Рет қаралды 122,736

Ben Awad

Ben Awad

Күн бұрын

Пікірлер: 136
@Ko0lHaNDLuKex
@Ko0lHaNDLuKex 5 жыл бұрын
You deserve more subscribers, Ben! You teach really well. Learnt a few things despite initially thinking this was just for beginners Hooks intro!
@deepakhk9439
@deepakhk9439 4 жыл бұрын
really awsome
@channel23653
@channel23653 4 жыл бұрын
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 жыл бұрын
I feel so upgraded after watching this tutorial. Thank you!
@TubeSwarn
@TubeSwarn 4 жыл бұрын
Much more information in this 15 mins of video than any other useState tutorials in KZbin
@mostinho7
@mostinho7 Жыл бұрын
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
@bantinggamer4962
@bantinggamer4962 5 жыл бұрын
i should have totally given this a watch ages ago. thanks Ben your the best!
@sathishkumarr7106
@sathishkumarr7106 3 жыл бұрын
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.
@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.
@zaretix
@zaretix 4 жыл бұрын
I feel like this is peak React/JavaScript. It's so intuitive and clean
@GL-fc2pv
@GL-fc2pv 5 жыл бұрын
the best explanations on youtube
@stoic2454
@stoic2454 3 жыл бұрын
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
@SomeGamerDudeGuy
@SomeGamerDudeGuy 5 жыл бұрын
Please continue this series really enjoy learning from you.
@meganadams7274
@meganadams7274 5 жыл бұрын
Best tutorial I've watched in a long time. I'm gonna guess you're a professional teacher irl haha
@bawad
@bawad 5 жыл бұрын
actually KZbin is the only place I teach, but I've made a lot of videos to practice
@Meskarts
@Meskarts 4 жыл бұрын
Thanks alot Dude! Was searching for this .. very nice spoken - explained! Please do more!
@bawad
@bawad 4 жыл бұрын
thanks, this is the rest of the series kzbin.info/aero/PLN3n1USn4xlmyw3ebYuZmGp60mcENitdM
@21agdmnm
@21agdmnm 5 жыл бұрын
so glad I found your channel! amazing tutorials! Subscribed !
@bawad
@bawad 5 жыл бұрын
welcome :)
@armanrozika
@armanrozika 4 жыл бұрын
Man, you're so fast when explaining, I have to pause the video and then think lol. Anyway great content, thanks!
@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
@mudandmoss4132
@mudandmoss4132 4 жыл бұрын
Bro your videos are sick, subscribed.
@func0der
@func0der 5 жыл бұрын
Clear explanation of React useState hook. Thank you very much :)
@ansonthedev
@ansonthedev 4 жыл бұрын
Jesus, you make me want to learn VIM just watching you go with the keybindings.
@bawad
@bawad 4 жыл бұрын
Do it benawad.com/vim
@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....!
@gosnooky
@gosnooky 5 жыл бұрын
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.
@thanatos454
@thanatos454 5 жыл бұрын
The playlist link in the description is to your Mini Google Docs Clone series.
@catrosits
@catrosits 4 жыл бұрын
Thanks.. your manner of explaining is awesome.. keep it up1
@alwinvillero4404
@alwinvillero4404 3 жыл бұрын
How do you code so freaking fast, almighty Ben?
@nachiketkanore
@nachiketkanore 3 жыл бұрын
vim
@markod3957
@markod3957 4 жыл бұрын
Wow dude, your content is fantastic!
@KbIPbIL0
@KbIPbIL0 5 жыл бұрын
man your tutorials are amazing
@bawad
@bawad 5 жыл бұрын
thanks :)
@pianoby40
@pianoby40 4 жыл бұрын
Thanks Ben. These series really helps me a lot of time understanding react mind 👍👍👍
@jlmonroy13
@jlmonroy13 4 жыл бұрын
what a clear explanation man! Thank you!
@kukuster
@kukuster 4 жыл бұрын
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
@NyKunPikaDasGalaxias
@NyKunPikaDasGalaxias 4 жыл бұрын
Ben, you're amazing! Thank you very much.
@mr.RAND5584
@mr.RAND5584 4 жыл бұрын
Thank you so much for sharing your knowledge. Amazing Tutorials.
@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
@nicken86
@nicken86 5 жыл бұрын
Thanks Ben! How would you handle the custom state hook in typescript?
@bawad
@bawad 5 жыл бұрын
I would probably do: gist.github.com/benawad/93f1734ba5771ea53615be6f6fd89721
@larsap
@larsap 5 жыл бұрын
@@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 5 жыл бұрын
feel free to post the code in my discord, and I'll take a look at it discord.gg/Vehs99V
@yehoshualeventhal5078
@yehoshualeventhal5078 4 жыл бұрын
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 жыл бұрын
Nvm, I figured it out. Thanks!
@leonardocamargo5412
@leonardocamargo5412 4 жыл бұрын
Thank you, for this playlist about React Hooks. Is Amazing.
@mandiprai8593
@mandiprai8593 4 жыл бұрын
Since the introduction of hooks, is it better to just use functional components compared to the class components?
@tkousek1
@tkousek1 3 жыл бұрын
This is great. Thank you so much.
@olegnamedia2355
@olegnamedia2355 4 жыл бұрын
Why is e.target.name enclosed between square brackets and not just single or double quotes?
@svetozarradic9667
@svetozarradic9667 4 жыл бұрын
Hey Ben, thank you for this wonderful hooks tutorial. Do you know maybe how to write tests for the components which use hooks?
@juancamiloq1
@juancamiloq1 5 жыл бұрын
Awesome series
@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 жыл бұрын
benawad.com/fast
@jessekrim
@jessekrim 3 жыл бұрын
How would you do this in React Native? What is the rn alternative for e.target.value ? Thank you!!
@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.
@Malcolm777-i
@Malcolm777-i 4 жыл бұрын
Love this channel !
@DevlogBill
@DevlogBill 2 жыл бұрын
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?
@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
@danieljing9319
@danieljing9319 5 жыл бұрын
Why use [ ] to close [e.target.name]
@bawad
@bawad 5 жыл бұрын
const name = "firstName" { [name]: "ben" } is equivalent to { "firstName": "ben" }
@danieljing9319
@danieljing9319 5 жыл бұрын
@@bawad thanks, that's ES6 syntax
@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?
@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?
@mongonode6755
@mongonode6755 3 жыл бұрын
Can anybody please explain me the return statement at 13:33?
@ibrahimalshubaily9520
@ibrahimalshubaily9520 2 жыл бұрын
Great! Thx Ben.
@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
@jacksmith870
@jacksmith870 5 жыл бұрын
5:05 I didn't get the side note. Any detailed explaination for it ben? Thanks.
@bawad
@bawad 5 жыл бұрын
try doing setCount(count + 1) setCount(count + 1) then try setCount(c => c + 1) setCount(c => c + 1)
@iamkiko5046
@iamkiko5046 5 жыл бұрын
You rock man, thank you.
@salemouail627
@salemouail627 4 жыл бұрын
on 7:14 how did u delete that comma without moving cursor ?
@bawad
@bawad 4 жыл бұрын
vim benawad.com/vim
@salemouail627
@salemouail627 4 жыл бұрын
@@bawad thanks so much And thanks for the turorial ♡
@dayashankartr
@dayashankartr 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 жыл бұрын
try setCount(c=>c+1) setCount(c=>c+1) vs setCount(count+1) setCount(count+1)
@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.
@CrusaderMen
@CrusaderMen 4 жыл бұрын
Thank you Ben!
@robertshawnmitchell
@robertshawnmitchell 5 жыл бұрын
Thought you were all in on Typescript
@bawad
@bawad 5 жыл бұрын
I am, but for some beginner stuff, I want to use javascript so it's more accessible
@jamols09
@jamols09 4 жыл бұрын
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.
@gevikb
@gevikb 5 жыл бұрын
Could you please explain a bit more about why the usage this useStare (useForm) is preferred over using Formik? Thank you :)
@bawad
@bawad 5 жыл бұрын
I prefer to use formik, just wanted to show how you could build your own
@gevikb
@gevikb 5 жыл бұрын
@@bawad Thank you.
@newrind
@newrind 5 жыл бұрын
Ben are you using brown switches?
@bawad
@bawad 5 жыл бұрын
yes
@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 жыл бұрын
onChangeText just gives you the text, so: (name, text) => setValues({...values, [name]: text })
@SahraClayton
@SahraClayton 4 жыл бұрын
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
@AmanNidhi
@AmanNidhi 5 жыл бұрын
is this part of a playlist ?
@bawad
@bawad 5 жыл бұрын
It's the start of one: kzbin.info/aero/PLN3n1USn4xlmyw3ebYuZmGp60mcENitdM
@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 3 жыл бұрын
@@CynthiaSotoCaballerolYEAHl OK, I was under the impression that hooks can be created only inside components.
@barryfulk6820
@barryfulk6820 5 жыл бұрын
I'm pretty sure Ben is a 10x developer ;)
@bawad
@bawad 5 жыл бұрын
😂
@nguyenchau7107
@nguyenchau7107 3 жыл бұрын
Damn I wish I know this channel earlier
@toponthelist9154
@toponthelist9154 4 жыл бұрын
Do you use Vim inside VS Code?
@bawad
@bawad 4 жыл бұрын
yeah
@ekbergpeter6987
@ekbergpeter6987 3 жыл бұрын
Just another one who puts up tons of good material, but unfortunately to busy to answer any questions.
@vvchernyshenko
@vvchernyshenko 4 жыл бұрын
Absence of setState callback function in useState hook is the biggest fail. It helped me so many times.
@abbisalmi1948
@abbisalmi1948 4 жыл бұрын
Thank you for this
@danianiazi8229
@danianiazi8229 4 жыл бұрын
'useState' is not defined anyone please?
@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 жыл бұрын
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.
@khamhti5223
@khamhti5223 2 жыл бұрын
Thanks
@saadbaig3060
@saadbaig3060 4 жыл бұрын
you got my sub
@necbottz
@necbottz 5 жыл бұрын
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 5 жыл бұрын
Have you tried cloning my repo?
@necbottz
@necbottz 5 жыл бұрын
no i didnt clone i was just folloeing maybe let me try that. By the way thanks for reply
@backtoislam610
@backtoislam610 2 жыл бұрын
Awesome
@trueMatthewkirkham
@trueMatthewkirkham 3 жыл бұрын
are you aware that web-dev-simplified basically just copied this video? I preferred yours though. Love your shit man.
@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 KZbin tutorials.
@bawad
@bawad 5 жыл бұрын
I feel ya
@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
@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
@5ervanttechintel_github_io979
@5ervanttechintel_github_io979 5 жыл бұрын
*So sad, you can't use TypeScript with React Hooks...*
@bawad
@bawad 5 жыл бұрын
You can
@vldblc9350
@vldblc9350 5 жыл бұрын
lol, why you can't?
@5ervanttechintel_github_io979
@5ervanttechintel_github_io979 5 жыл бұрын
@@TruthVideosOnline The purpose of functional component is to write less code.
@davidfromnorth7836
@davidfromnorth7836 4 жыл бұрын
10:59 oops
@johnnwabuforudemezue1108
@johnnwabuforudemezue1108 3 жыл бұрын
You made this very simple stuff a complete sham,God help you
@alexsilny5748
@alexsilny5748 5 жыл бұрын
Great video tutorial. Thanks. It helped me get an understanding of Hooks. I do have a question? In kzbin.info/www/bejne/b6nLfHtqaNGfnrc you are returning an array in the useForm Hook. Can you also give an example of how you would return an object?
@bawad
@bawad 5 жыл бұрын
return { values, handleChange: e => {} }
React Hooks useEffect Tutorial
20:15
Ben Awad
Рет қаралды 157 М.
React Hooks useMemo Tutorial
11:21
Ben Awad
Рет қаралды 78 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
React Hooks Crash Course (useMemo, useCallback and more).
31:49
developedbyed
Рет қаралды 88 М.
Beginner React.js Coding Interview (ft. Clément Mihailescu)
36:31
Ben Awad
Рет қаралды 2,2 МЛН
Custom Hooks in React (Design Patterns)
12:56
Cosden Solutions
Рет қаралды 56 М.
Why you need hooks and project
19:27
Hitesh Choudhary
Рет қаралды 21 М.
React Hooks useReducer Tutorial
17:16
Ben Awad
Рет қаралды 91 М.
React.js Hooks Crash Course
1:02:54
Academind
Рет қаралды 317 М.
React Hooks useCallback Tutorial
11:15
Ben Awad
Рет қаралды 115 М.
React Hook Form - Complete Tutorial (with Zod)
28:22
Cosden Solutions
Рет қаралды 141 М.
Introducing React Hooks
20:50
Traversy Media
Рет қаралды 345 М.
Colin Gray - Default to performance (Chain React 2023)
38:49
Confreaks
Рет қаралды 2,9 М.
Beat Ronaldo, Win $1,000,000
22:45
MrBeast
Рет қаралды 158 МЛН