No video

Top 6 React Hook Mistakes Beginners Make

  Рет қаралды 567,926

Web Dev Simplified

Web Dev Simplified

Күн бұрын

FREE React Hooks Simplified Course: courses.webdev...
📚 Materials/References:
FREE React Hooks Simplified Course: courses.webdev...
Reference vs Value Video: • Reference Vs Value In ...
Reference vs Value Article: blog.webdevsim...
🌎 Find Me Here:
My Blog: blog.webdevsim...
My Courses: courses.webdev...
Patreon: / webdevsimplified
Twitter: / devsimplified
Discord: / discord
GitHub: github.com/Web...
CodePen: codepen.io/Web...
⏱️ Timestamps:
00:00 - Introduction
00:39 - Using state when you don’t need it
02:57 - Not using the function version of useState
06:44 - State does not update immediately
08:25 - Unnecessary useEffects
12:40 - Referential equality mistakes
16:55 - Not aborting fetch requests
#WebDevelopment #WDS #JavaScript

Пікірлер: 479
@IceMetalPunk
@IceMetalPunk Жыл бұрын
React's own docs explicitly recommend using state-controlled inputs over refs whenever possible. It refers to the ref method as "quick and dirty" because it lets the DOM handle tracking the value instead of React, which can sometimes cause unexpected behavior when React renders its virtual DOM. So... yeah. I think for forms, especially large ones, it's better to keep track of values in a single state object with key-value pairs. That does mean it'll re-render whenever a value changes, but since React only manipulates the DOM for the one input that's changed, it's not a big deal; and it allows React to have more control over, and more understanding of, the inputs, for optimizations. The main issue with using local variables instead of useEffect for composite values is future-proofing: when you add more state to the component, those variables will be re-evaluated on every re-render even if they don't need to be. In such cases, I think useMemo is the optimal solution; in fact, it's why useMemo exists! (And I believe recomputing a memoized variable doesn't trigger a re-render the way setting state does, though I couldn't find anything definitive about that.) But you are right that in some cases, you don't need to listen for changes at all, since you can just use the new value at the same time as you're setting it on the state.
@SahraClayton
@SahraClayton Жыл бұрын
I think useRef should be used for forms, you don't want component re-rendering on every key stroke just for a form, but if you was using a search/filter input where you are filtering on the users key stroke then you would need to useState and make it a controlled component.
@chonmon
@chonmon Жыл бұрын
@@SahraClayton It only re-rendered the input DOM tho? Instead of the whole form if I understand it correctly. I don't think it's a big deal.
@ccrsxx
@ccrsxx Жыл бұрын
@@chonmon yes, not to mention you'll always want to have some kinda validation on the form.
@SahraClayton
@SahraClayton Жыл бұрын
@@chonmon it's not a big deal but if you can avoid any components from rerendering no matter how small is just a bonus. Also it's less syntax in that component, which looks nicer
@thienhuynh7962
@thienhuynh7962 Жыл бұрын
@@SahraClayton then you can opt for other event like submit or blur to minimize re-render. Although useRef is a fine solution since there’s no re-render, using it with forms that have many fields or require validation will give you a hard time managing these fields’ value and their behaviors
@CarlosRenanFonsecadaRocha
@CarlosRenanFonsecadaRocha Жыл бұрын
A good thing to point out about the first mistake is that a good UX informs the user if the field has any errors while they are typing; in this case, this is not possible. Better to stick with states, but it is a nice thing to know.
@mehmetacikgoz9814
@mehmetacikgoz9814 Жыл бұрын
Using refs instead of state does not seem right in this scenario. First of all, such a render does not create any overhead since behind the scenes react will do equivalent of what you have done with refs. Besides, these code will probably be improved with validators etc. which will result going back to state version
@rvft
@rvft Жыл бұрын
Sus lan
@neutralface
@neutralface Жыл бұрын
You're correct. React has always preferred controlled components (react handles input value) over uncontrolled components (DOM handles input value). This is second time I see Kyle making this counter-argument to use refs, which I think is incorrect.
@ACMonemanband
@ACMonemanband Жыл бұрын
This is really debatable, as the scenario Kyle mentioned is that, all the inputs are only used upon form submission. For experienced React developers, we have always been using controlled components and get really used to it, so we are very unlikely to use Kyle's uncontrolled ways in any circumstance.
@neutralface
@neutralface Жыл бұрын
@@ACMonemanband True. Refs are mentioned in the "Escape hatches" section in the new react docs, which for me intuitively tells that refs are used as secondary option when first option of controlled components doesn't work.
@rand0mtv660
@rand0mtv660 Жыл бұрын
​@@ACMonemanband These days I usually just use react-hook-form and don't worry if something is controlled or uncontrolled in a most forms. The fact is that for more complex forms that might compute a lot of stuff, uncontrolled form inputs are better because they won't re-render the whole form on each input change. If you need to do any active validation, you'll probably have to go for controlled forms because you'll have to react to form changes all the time and not just on submit, but in cases where you just need to validate on submit, you probably want to go for uncontrolled inputs. Only reason I dislike his input ref example is because you'll rarely only work with two field forms in the real world and you'll most likely have multiple (more complex) forms in your app. At that point, just reach for something like react-hook-form and never worry about building forms in React.
@jackwin9641
@jackwin9641 Жыл бұрын
well this is what I am looking for ages. THANKS KYLE, you made my day, now I can revise my old code at a higher level
@pedrocruz8164
@pedrocruz8164 Жыл бұрын
some of the senior developers in a company I worked for used to not approve my PRs asking for me to not use useRef to the store any state in react component. It is nice to see someone explaining why not every state needs to be rerendering the component on every data update.
@runonce
@runonce Жыл бұрын
For handling form why not simply grab the input values from the onSubmit event? No need for state or refs.
@twothreeoneoneseventwoonefour5
@twothreeoneoneseventwoonefour5 Жыл бұрын
Yes that is what I usually do. const formData = new FormData(e.currentTarget) and then const [username, password] = [formData.get("username").toString(), formData.get("password").toString()]; looks cleaner to me than using refs or state there well I still use state for input validation though
@paulbird2772
@paulbird2772 Жыл бұрын
Very clear video, I just switched to Typescript for last major project, took a bit of effort initially but rewards are great, the build step catches a good few errors very early. I think not using TS could be added to your list. I expect many that haven't given it sufficient time will disagree.
@dannyj7262
@dannyj7262 Жыл бұрын
I spent hours on my degree final project sorting out errors that js didn't pick up until I tried to run a piece of code with an error in. Switched to TS and it's a million times better and Id also say I've learnt a lot more too
@adnanhaider9302
@adnanhaider9302 Жыл бұрын
The number of useEffect gotchas and permutations are just never ending. I have such a love hate relationship with this hook.
@coderyan
@coderyan Жыл бұрын
You and me both
@stivenmolina4075
@stivenmolina4075 2 ай бұрын
I have a relation of pure hate with that hook
@asmmisfar
@asmmisfar Жыл бұрын
Hey Kyle, This is really very helpful for me. Tomorrow I have a task to complete in my office. I was worried about how to do that. but, this video gave me a clear idea about that. Thanks a lot. Keep going, bro. Loves from Sri Lanka ❤
@taulantus
@taulantus Жыл бұрын
For the first case you don’t need state or refs the onSubmit function has access to all the input fields via the event parameter
@adtc
@adtc Жыл бұрын
Good video. One tip: if the arrow function will return an object, you can just put the object inside parentheses instead of using a full code block with a return keyword. Example: () => ({ age, name }) Also I believe you can simply return controller.abort instead of returning an anonymous function which calls controller.abort()
@billynitrus
@billynitrus Жыл бұрын
Would you be able to explain to me what is being passed to currentCount at around 6:00? Does calling setState automatically pass in the current state as an argument? Not sure how else it would be possible since the parameter name seems to be arbitrary, ie prevState. It sounds like you know your stuff, any help would be appreciated!
@zakariaibrahim3750
@zakariaibrahim3750 Жыл бұрын
@@billynitrus setState accepts a function which its parameter (currentCount) is the value of ‘count’ before being updated. Ex. If count is 9 and you clicked on the ‘+’ button ,if you try to log ‘currentCount’ you would get 9 ,thus ‘return currentCount (9) + amount(1)’ will result in updating ‘count’ to 10
@bowenp01
@bowenp01 Жыл бұрын
Fantastic! I’m just learning react and you’ve explained the funky behaviour I’ve been getting with useState perfectly. Thanks for taking the time to make these videos 😊
@rubyvi7308
@rubyvi7308 Жыл бұрын
Why are you excited to watch while the other crying are u happy
@Caspitein
@Caspitein Жыл бұрын
Interesting video Kyle! I noticed I do make a few of these mistakes myself. I think the problem is that us developers can be a bit "lazy" learning new frameworks. I understand how the useState and useEffect hooks work and I never took the time to learn about useRef and useMemo for example.
@coderyan
@coderyan Жыл бұрын
totally!
@Spyrie
@Spyrie Жыл бұрын
You'll use it once you handle big data and use expensive functions that can block the main thread
@d.ilnicki
@d.ilnicki Жыл бұрын
Well I this case the "lazy" one is the video author. Recommending using refs over state on 1.34M subscribers is a crime.
@WorkThrowaway
@WorkThrowaway 3 ай бұрын
This was a great video and helped me solve an issue i had with my hook, namely having multiple states update with an action needed once they were both updated. it took a while to wrap my brain around it, but this video really helped give me the vision. love your videos and that you go super in depth (in the longer ones). probably the best coding tutorials on youtube. if i ever get my web dev dream job, i will be getting you a few coffees/beers.
@mikejuli8243
@mikejuli8243 Жыл бұрын
Man, using setCount( (count)=>{count + 1} ), it's the greatest way how to handle this real react problem! That's was extremely beneficial for me !!!!! Thank you!
@FrancisBourgouin
@FrancisBourgouin Жыл бұрын
Pretty great tips for React! However, I have a couple of disagreements with your mistakes. Maybe I'm wrong, but I'd be curious to discuss with others here: 1- useRef vs useState for forms: Using state to update and rerender form inputs is what a controlled form is. If you use useRef you can't apply conditions or mutations on the inputs, you'll trust your DOM into having the right information instead of your state. So even if it works, if you need a form, use a state, not a ref. 2- prev vs count: I disagree with using prev everywhere, especially with the example that you're using. When I use count, I know what count is, all the time. if I use prev, I need to look through my entire code between the state and my function to 'count' how many times did I call setCount. Also, you should do all the work, then call a setState, not do a setState party in my opinion. Continue the great work!
@saikatpaul6576
@saikatpaul6576 Жыл бұрын
Learn the basics dude.. What he did with the count is exactly how you should do it
@FrancisBourgouin
@FrancisBourgouin Жыл бұрын
@@saikatpaul6576 Very productive comment there, care to elaborate on why I'm wrong ?
@saikatpaul6576
@saikatpaul6576 Жыл бұрын
@@FrancisBourgouin you might need to setState on numerous conditions.. How are you going to do that? The way you are talking about is not possible because that way you can use setState only once. Say it's an array of object. And you may want to manipulate it on different conditions
@FrancisBourgouin
@FrancisBourgouin Жыл бұрын
@@saikatpaul6576 I would disagree with what you just said, there's always a easy way to do it with one setState. I'd be curious to see what you would find impossible to do, I'll show you how I would write it.
@saikatpaul6576
@saikatpaul6576 Жыл бұрын
@@FrancisBourgouin it's not impossible.. But that's the right method..
@eiatot6455
@eiatot6455 Жыл бұрын
{} === {} false is the correct result for more than one reason but what I want to stress is that {} is NOT an object, it is the LITERAL CONSTRUCTOR hense don't tide the symbols to what they actually are, the constructors return objects. Excellent video ty.
@AR7editing
@AR7editing Жыл бұрын
15:45 in that case instead of useMemo we can directly use in the dependency array person.name and person.age and it will not cause rendering when you change the dark mode checkbox
@jennifermagpantay7933
@jennifermagpantay7933 Жыл бұрын
Golden tips! Love content like that with different case scenarios and clear explanation! I have learnt so much! Thanks for sharing!
@deadlock107
@deadlock107 Жыл бұрын
It was worth to watch, I learned pretty valuable things especially the fetch abort, it's golden, thank you!
@RafaeruC
@RafaeruC Жыл бұрын
if you want to use the AbortController, but you are fetching data in a function, instead of in a UseEffect. The way i do is, use a UseRef to save your AbortController, use controller.abort() in the beginning of the function , then create a new controller and assign it to ref.current. pass the new signal to the fetch and voilà.
@elmalleable
@elmalleable Жыл бұрын
if its a simple form then you can just take the values from the form event when you submit and you dont really need to keep state provided you have a vary basic use case for the form, complex validations and connected models then you'll probably go back to using state
@anhqui19822011
@anhqui19822011 5 ай бұрын
Thank you for the great video! Regarding the choice between useRef and useState for form submission, I believe we should use useState instead of useRef. This is because we typically need to reset the input value to empty after submission. For input filtering, updating the state as the keystrokes change is necessary to display the latest value in the UI.
@letsoverengineer
@letsoverengineer Жыл бұрын
Hands down, what a perfect rhetoric - watched it with great pleasure - thanks
@sergiogusto
@sergiogusto Жыл бұрын
00:45 tag stores fields value
@mangman
@mangman Жыл бұрын
Great video! useRef seems neat, but you do lose the ability to have real time validation, as that logic would be placed in the onSubmit function.
@user23724
@user23724 Жыл бұрын
You really have a talent for education my man. Whenever I'm not understanding something in web dev now I just search for web dev simplified and boom there's always a video on it. Thank you! These were really good tips for those of us just getting into front end :)
@Toomda
@Toomda Жыл бұрын
This was soo helpful. I had a beginner project, just for fun and I almost made all of the mistakes. I just fixed my code, and it looks much better now. Thank you
@loicrutabana1884
@loicrutabana1884 Жыл бұрын
Amazing! You make great content. I especially like your youtube shorts
@saheedsimisaye8978
@saheedsimisaye8978 Жыл бұрын
Most valuable React tips I have learnt regarding some bad code practices I have been applying in React. Thanks for this very comprehensive video with lots of valuable information covered in just over 20mins.
@nsontung
@nsontung Жыл бұрын
one of the best React tips I ever learn on KZfaq. Thank you so much
@DarkFlarePrince
@DarkFlarePrince Жыл бұрын
This is the single most useful video I've seen since I started React coding
@Bekonisko
@Bekonisko Жыл бұрын
In the last example, @WebDevSimplified are you sure the aborting would "ignore" the `then`, `catch` and `finally`? I am pretty sure aborting this way would still fire `catch` with `error` being `DOMException: user aborted the request.` and `finally` setting the `loading` to `false`, or wouldn't it?
@evgeniyd7957
@evgeniyd7957 Жыл бұрын
For email/password you don't need refs, just grab values from the form. Yeah, inputs should have name attributes to be added
@ogs3721
@ogs3721 Жыл бұрын
You are wrong about using useRef for managing form values. It's even mentioned by one of the react developers at Facebook. You can even find this in the react beta docs. useRef is overly used by people who have used Vue js before. Well, react is different than Vue js
@jael1399
@jael1399 Жыл бұрын
can you elaborate or reference the beta doc page about handling form values? what's the alternative then?
@belkocik
@belkocik Жыл бұрын
amazing video for react beginners. :) Thank you. Looking forward for more react mistakes that beginners and more advanced devs make
@jesuscabrita5815
@jesuscabrita5815 Жыл бұрын
that useMemo trick... was lovely
@kamelnazar-instructionalde9740
@kamelnazar-instructionalde9740 Жыл бұрын
This worked incredibly well! I can finally play it thanks
@chanhaoen62
@chanhaoen62 Жыл бұрын
Thanks for all the react tips! Really helped me a lot :) will you ever put out a video of you playing guitar? haha
@lukas.webdev
@lukas.webdev Жыл бұрын
I'm pretty sure, that there is already a part in one of his videos, where he plays a solo... But I can't remember how it was called... 😄
@beinghappy3924
@beinghappy3924 Жыл бұрын
You really deserve thumbs up from me. Good Job. 👍
@tsooooooo
@tsooooooo Жыл бұрын
First example is iffy advice for beginners. They won't be able to discern when they can get away with refs. It's better to use state by default because it's reactive. Going straight to refs is premature optimisation. What if your form has fields that should conditionally be shown depending on the value of other fields? You need to re-render. It's a pretty common case. The first useEffect example is so spot on though. I see that all the time. And the referential equality problem is so subtle but so important to note! Nice work.
@shivshankarshejwal6728
@shivshankarshejwal6728 Жыл бұрын
Nice tip last one.Thanks
@shashi20008
@shashi20008 Жыл бұрын
Well, like others in the comments have pointed out, useRef over useState shouldn’t be taken as a general rule. Aamof, for the example discussed in the video, it might be best to leave the inputs as uncontrolled components if their values are not explicitly used. The FormData API can be conveniently used in the submit event handler to get access to actual values at time of submit. That said in non trivial examples it’d typically not be the case as you’d want client side error handling and what not. Just use useState then. Also regarding the last suggestion in the video, the behaviour described for AbortController is not accurate. Aborting a request using AbortController doesn’t prevent the promise returned from fetch() call from settling. The promise will actually be rejected with DOMException having code ‘AbortError’. While cancelling previous requests in this manner you’d want to explicitly ignore these errors (and not display them to users as error). An alternate method I have used to solve the same problem is to keep track of start time of the current request (via closure), and start time of most recent finished request (via ref). When a response arrives, check it’s start time against most recently finished request’s start time, if it’s not later just ignore the response. Sounds complicated but can easily be wrapped in a small lib function.
@achyutrastogi8080
@achyutrastogi8080 Жыл бұрын
Is there any inherent benifit to using your mehod over useMemo the way Kyle has shown? The only downside I see is having to manually ignore code 'AbortError' DOMExceptions so that they don't clutter your health tracking tools.
@patemanauli3573
@patemanauli3573 Жыл бұрын
if you are so sure that your url changes very quickly after another, I prefer using debounce instead rather than executing and canceling a fetch.. no request is faster than a cancel and re-request, canceling doesnt work if your API runs fast enough
@sexylittlewilly
@sexylittlewilly Жыл бұрын
this video is all react people need, thanks a lot.
@magicfibre
@magicfibre Жыл бұрын
IIRC, setState is not asynchronous. The reason the "count" variable is not updated is because it doesn't directly reference the actual state. "count" references a copy of whatever was in the state when useState was called. Other than that, this was a great video! I still catch myself forgetting to use the function version of useState, so hopefully I'll remember this video the next time I do! (Edit) see the comments for more details!
@dirkpostma77
@dirkpostma77 Жыл бұрын
It actually is kind of asynchronous, though not same as in promises. Usually the setstate is executed on next render. However, state changes can also be batched, which results in kind of asynchronisity
@magicfibre
@magicfibre Жыл бұрын
@@dirkpostma77 Good to know, thanks!
@okamimoushiyou
@okamimoushiyou Жыл бұрын
@@magicfibre do you mind updating your comment to reflect this newfound info? What Dirk said is correct, in that setState isn't your traditional "async/await" kind of asynchronous (likely what you were referring to), but it does operate asynchronously and is exactly why what happened in the video happened. If you test it yourself, further testing will show the console log is always one step behind state due to this asynchronousity. Eg. It has nothing to do with referencing the first assignment of state in the useState; which I think is what you were referring to, correct me if I'm wrong. Just would hate for a newbie to come across your comment and take it at face value and not get the info Dirk mentioned.
@magicfibre
@magicfibre Жыл бұрын
@@okamimoushiyou Sure!
@Krzysiekoy
@Krzysiekoy Жыл бұрын
@@okamimoushiyou I don't think this is right. Console log is one step behind because it references current state value as a snapshot for that particular render. From the new docs: "A state variable’s value never changes within a render, even if its event handler’s code is asynchronous. Inside that render’s onClick, the value of number continues to be 0 even after setNumber(number + 5) was called. Its value was “fixed” when React “took the snapshot” of the UI by calling your component. " // number is 1 right now // hey react, update the number value FOR THE NEXT RENDER // OK - GOT IT BOSS setNumber( (n) => n +1 ) // The value of number is "fixed" for this render - we are passing a "snaphshot" of current state, for the current render to the console.log console.log(number) // 1 // You could even do this // We are still passing a snapshot of the current state for the current render so the log will be "behind" or in other words, it will use the state snapshot for that particular render setTimeout( () => console.log(number), 10_000) // 1 Maybe we are talking about the same thing, but I really like the explanation from new new react docs. Talking about state as being "asynchronous" (regardless of your interpretation of what "async" means in this context) is confusing. I much prefers the docs' analogy of "Snate as a Snapshot"
@mengfandy7365
@mengfandy7365 Жыл бұрын
i love react and i love this youtube channel, hi from indonesia
@charlesvicencio2461
@charlesvicencio2461 Жыл бұрын
Thank you Kyle!
@jonasex3001
@jonasex3001 Жыл бұрын
Everything is so clunky in React... Unfortunately I have to work with it, but vue is on another level.
@goyii
@goyii Жыл бұрын
The first case. Is just simplier and better to put names on the inputs. Then when submiting, get all inputs inside the form and reduce them into a single object. const inputs = Array.from(target.getElementsByTagName('input')) const values = inputs.reduce((a,{name, value,...rest}: HTMLInputElement) => ((value && name) ? {...(a as object), [name]: rest.hasOwnProperty('checked') ? rest.checked : value} : a),{}) onSubmit(values, event.target). What this cause is that you could wrap it into a 'Form' component and re use it every time you need it. And its dynamic, if you add an input to the form with a given name, the value of that input will be in the values object. No need to repeat yourself with each field
@developer_hadi
@developer_hadi Жыл бұрын
Thank you so much, as a beginner you helped me a lot
@joemathan6101
@joemathan6101 Жыл бұрын
You would still control re-renders by using denounce on top of state. So, there won't be a need for ref in this case. As the react doc itself suggests us to go with controlled components over uncontrolled components Useful video thank you
@roberthall9452
@roberthall9452 Жыл бұрын
Don't even need the refs if you serialize the form data on submit using `const formData = new FormData(e.target);` and `const formProps = Object.fromEntries(formData);` Just make sure to give all your fields a name attribute and you're good.
@darkmift
@darkmift Жыл бұрын
Excellent explanation on state setter usage
@NathanBudd
@NathanBudd 11 ай бұрын
That last useFetch insight is very helpful! I think it could be even more so if combined with some king of debounce? So the fetch only runs after say 0.5s?
Жыл бұрын
You just prevented some lay offs, great job.
@gearmeout
@gearmeout Жыл бұрын
There is a use case for the functional approach to useState and it is when you have a specific need to update and then read/handle state value again within a singular event. It's a great technique, but it is not the only way and its existence does not make the other options wrong. I know it was just for example, but there was no problem with how the counter example was coded originally and it gained nothing by being refactored. The argument made in this video is like saying that its wrong to ride a road bike because a mountain bike is better at jumps. One is simply intended for navigating different types of terrain than the other.
@igorkushnir4966
@igorkushnir4966 Жыл бұрын
last one just perfect! thanks!
@drelroy
@drelroy Жыл бұрын
All good, thanks, but it's really wierd to see that you don't use semicolons - it's a direct way to have errors.
@darkthrongrising5470
@darkthrongrising5470 Жыл бұрын
I've been tinkering with React here and there for about five years, this is the first I'm hearing of a functional version of useState.
@wisdomelue
@wisdomelue Жыл бұрын
best explanation of useMemo out here👏🏽
@AllAboutDev
@AllAboutDev Жыл бұрын
Last tip was really important ❤
@dysb
@dysb Жыл бұрын
I'd like to see more use of spreading, `onChange(({ target: { value } }) => setName(value))`, and implicit return, `setCount((c) => c + amount)`. Get people used to seeing them and help them write cleaner code.
@backupmemories897
@backupmemories897 6 ай бұрын
hmm I don't have this problem in angular xD but i'm trying to learn react this month. this is a good help
@KathidFX
@KathidFX Жыл бұрын
It does work as the same as before in case of email and password, but we use usestate , value and onchange to create Controlled components. Uncontrolled components with refs are fine if your form is incredibly simple regarding UI feedback. However, controlled input fields are the way to go if you need more features in your forms.
@louis-emmanuelisnardon8308
@louis-emmanuelisnardon8308 Жыл бұрын
Great video ! Well explained ! Thank you, I'm modifying my code right now...
@miguelsalesvieira
@miguelsalesvieira Жыл бұрын
This is the first time ever I comment on a KZfaq video but I think I should for the first time. I’ve been using React for the past 5 years and the “mistakes” shown in these video are no mistakes at all. They are perfectly valid and in my opinion preferred because it has a more pragmatic syntax. Yes, the alternatives can be considered (not in all cases) more efficient but these are not mistakes.
@wfl-junior
@wfl-junior Жыл бұрын
For the first example, you don't even need refs, you can capture the input values on the form submit event if you give the inputs a name attribute. const data = new FormData(event.currentTarget) keys will be the input names
@tomasburian6550
@tomasburian6550 Жыл бұрын
You are my hero. Btw, I think that we all learned the hard way that adding the useState count twice in a row would still do it just once :P
@hsmtrue
@hsmtrue Жыл бұрын
1. U don't need ref))) e.target.elements.email.value - is solution on vanilla javascript, but u need to set name attribute. it is direct value
@iuritorres
@iuritorres Жыл бұрын
which way is a better practice? create components by: export function Compoent() {}; or export const Component = () => {}; ?
@lukas.webdev
@lukas.webdev Жыл бұрын
export const Component = () => {}; This is the latest and also the better approach... 😉
@gilsonconceicao5201
@gilsonconceicao5201 Жыл бұрын
Thanks. I didn't knew these.
@patemanauli3573
@patemanauli3573 Жыл бұрын
you don't need id just wrap your input with your label use name on the input component {`Email`} you don't even need ref you can take all the form values from the form submit event const form = e.target; form.elements['name'].value
@evrentank6402
@evrentank6402 Жыл бұрын
Thanks a lot. These are very important and useful knowledges.
@komalmadamwar710
@komalmadamwar710 Жыл бұрын
this is super helpful
@TawfeekAmro
@TawfeekAmro Жыл бұрын
Actually, You don’t need a Ref also, what you need is a unique name for every input then use form data. let formData: FormData; formData = new FormData(e.target); const formProps = Object.fromEntries(formData); all the inputs will be converted to objects.
@kacperzmijak1462
@kacperzmijak1462 Жыл бұрын
Please don't do that in React, thank you
@sidbee604
@sidbee604 Жыл бұрын
@@kacperzmijak1462 Now tell us why instead of acting like you dropped the ultimate truth with nothing to back it up.
@aaqibhamdule73
@aaqibhamdule73 9 ай бұрын
u r a genius ! I want to do a full course u offer , but I can't afford it.😢
@brenosantin9739
@brenosantin9739 Жыл бұрын
6:50 - 8:20 is a golden tip for beginners
@mr.gandalfgrey2428
@mr.gandalfgrey2428 Жыл бұрын
Soo good to see that there's finally more than just a guitar in your room :D just kidding, awesome video as always!
@nillerbjj1
@nillerbjj1 Жыл бұрын
Excellent video, thank you very much.😊
@Pareshbpatel
@Pareshbpatel Жыл бұрын
Common React Hook mistakes, so clearly explained. Thanks, Kyle {2022-10-16}, {2022-11-01}
@vaibhavsri.
@vaibhavsri. Жыл бұрын
Really useful one. Please keep on making great informative videos like this. You're the best!
@AngusMcIntyre
@AngusMcIntyre Жыл бұрын
Deeply amuses me that overuse of hooks has put us back into event driven callback hell. This is the exact code scattering that we were trying to avoid with functional components
@georgethenewbie
@georgethenewbie Жыл бұрын
this is why everyone's running away from React.
@AngusMcIntyre
@AngusMcIntyre Жыл бұрын
@@georgethenewbie new toys don't solve talent problems. The symptoms described in this video are products of overloaded components. Move that code somewhere else and the semantics become clear. The functional paradigm can be very elegant but we have to put some effort in
@marwenlabidi_dev
@marwenlabidi_dev Жыл бұрын
thank you for this great quality content
@JonathanAdami
@JonathanAdami Жыл бұрын
I'll be honest, I started this video thinking I wouldn't learn anything and I did learn a couple of things! It's bit slower pace than I'd like but I'm sure the format is perfect for wider audience. Thanks for making it, and thanks for this content, much appreciated!
@joseff5998
@joseff5998 Жыл бұрын
I really appreciate your explanations. Top-notch!.
@aes0p895
@aes0p895 Жыл бұрын
yes, beautiful nerds do exist. anyway, you're actually one of the top 5 (quickly becoming top 2) i've found on this site for react stuff, and i'm expecting it's the same elsewhere. nice work buddy.
@vkeynez
@vkeynez Жыл бұрын
16:16 Instead of using useMemo in this scenario can we just put those two objects of name and age in the useEffect's dependency
@maximpolsky2683
@maximpolsky2683 Жыл бұрын
Many thanks! Good luck with your channel! Greetings from Kyiv!))
@Infinizhen
@Infinizhen Жыл бұрын
Great video. I don't know what more can i say. Cheers!
@nirmesh44
@nirmesh44 Жыл бұрын
best tips ever. i also was not using functional way of setting state
@FacuCarbonel
@FacuCarbonel Жыл бұрын
Adding on what's people saying about point #1; if you use useRef instead of useState while using Typescript would complicate things while asinging the proper types making it more easy and clean to just use useState
@jaroslavhuss7813
@jaroslavhuss7813 Жыл бұрын
The last one did not know... Thanks buddy...
@danielcettour
@danielcettour Жыл бұрын
i'm a react native developer and i commit some of this mistakes, thanks for this video
@sethwright2518
@sethwright2518 8 ай бұрын
In the first example when you simplify the input values with useRef() and onSubmit().. at that point .. could you not just destructure the values from the submit event itself? I wonder if theres another example that could work more effectively?
@trinhducthang6102
@trinhducthang6102 Жыл бұрын
I had a question about aborting a request in final exam. Though my teacher had explained me the necessary of canceling the request when component unmounts I understood vaguely. It's clear now thanks for your well explanation about the last mistake
@radhouze2554
@radhouze2554 Жыл бұрын
Subbed for that beautiful Jackson guitar
@ukaszsi1413
@ukaszsi1413 Жыл бұрын
Overall really good video so ofc there' a like from me but for the last issue the explanation was a bit too shalow and I didn't get it, maybe you coule extend on that in a separate vid with actual examples and more context? Anyway thanks for the knowledge sharing!
@kuken72
@kuken72 Жыл бұрын
This was really straight to the point and very helpful. Thank you Kyle! :)
@hazemalabiad
@hazemalabiad Жыл бұрын
You are AMAZING ... Kyle 💐
Most Beginner React Developers Do This Wrong
13:47
Web Dev Simplified
Рет қаралды 230 М.
3 Beginner React Mistakes That Can Ruin Your App
20:19
Web Dev Simplified
Рет қаралды 103 М.
No empty
00:35
Mamasoboliha
Рет қаралды 12 МЛН
Fast and Furious: New Zealand 🚗
00:29
How Ridiculous
Рет қаралды 48 МЛН
Little brothers couldn't stay calm when they noticed a bin lorry #shorts
00:32
Fabiosa Best Lifehacks
Рет қаралды 17 МЛН
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 469 М.
Learn TypeScript Generics In 13 Minutes
12:52
Web Dev Simplified
Рет қаралды 252 М.
All useEffect Mistakes Every Junior React Developer Makes
22:23
The Heart of React || How React works under the hood
10:32
AI Bruise
Рет қаралды 19 М.
Goodbye, useEffect - David Khourshid
29:59
BeJS
Рет қаралды 498 М.
Learn React useReducer Hook with Examples
14:19
Lama Dev
Рет қаралды 173 М.
Speed Up Your React Apps With Code Splitting
16:50
Web Dev Simplified
Рет қаралды 377 М.
Learn useReducer In 20 Minutes
20:12
Web Dev Simplified
Рет қаралды 501 М.
Stop Doing this as a React Developer
12:27
CoderOne
Рет қаралды 162 М.