5 Custom React Hooks You Need In Every Project

  Рет қаралды 169,588

Web Dev Simplified

Web Dev Simplified

Күн бұрын

Пікірлер: 181
@tevinmorake8924
@tevinmorake8924 3 жыл бұрын
An unbeliezvaly useful hook . Nice save @Web Dev Simplified at 7:09
@austinwhitbeck3362
@austinwhitbeck3362 3 жыл бұрын
Just wanted to say that I'm really appreciative of your videos! Im going through a front end bootcamp at the moment and having succinct well explained bits and pieces of information that's easy to digest has helped/continues to help me a lot. Thank you so much!
@HaykelBENJEMIA
@HaykelBENJEMIA 3 жыл бұрын
Regarding useTimeout, I think it should have an effect to clear the timeout when the component is unmounted.
@HaykelBENJEMIA
@HaykelBENJEMIA 3 жыл бұрын
@@PetrSkorepa you're right! As the effect hook has dependencies, I thought the cleanup function would only run if those dependencies change, but actually all cleanup functions are run on unmount, no matter the dependencies. Thanks for the hint, helped me correct a misunderstanding 👍
@sudarshankj
@sudarshankj 3 жыл бұрын
At 8:15, shouldn't we do 'callback()' instead of 'return callback()' ?
@zevspitz8925
@zevspitz8925 Жыл бұрын
7:54 useUpdateEffect is triggered too early when in strict mode.
@princebhanushali6255
@princebhanushali6255 10 ай бұрын
00:32 => 😃👍 , 01:58 => 🤯🤯
@gasparRaduB
@gasparRaduB 3 жыл бұрын
@11:00 those `slice` methods don't require `a.length - 1` as the second argument, if you use `a.slice(index + 1)` it's going to default to slicing the array from that index until the end - either than that, really useful hooks - I also use them all the time
@terryq5995
@terryq5995 3 жыл бұрын
and if the second argument was used, was it should just be a.slice(index + 1, a.length)? Because If a.length - 1, it would point to the last element of the array and slice method would not include that element into the new array?
@gasparRaduB
@gasparRaduB 3 жыл бұрын
@@terryq5995 that's correct - you'd miss the last element in the array, but that might be desired if you're doing an "undo" action - I'd be similar to a ".pop()" with the exception that "pop" actually modifies the original array, whereas "slice" will produce a new array (a clone if you will)
@user-fg6ng7ej6w
@user-fg6ng7ej6w 2 жыл бұрын
great series of explanations of hooks
@MiSt3300
@MiSt3300 3 жыл бұрын
brilliant... simple, and understandable, yet so brilliant! You teach me how to code like a true artist! Thanks Kyle!
@krige
@krige 3 жыл бұрын
9:54 there seems to be a problem with update and remove: when you update or remove the last element is removed as well
@jdawg011873
@jdawg011873 3 жыл бұрын
Change the "a.length - 1" in both of those functions to simply "a.length"
@sandeeptottadi
@sandeeptottadi 3 жыл бұрын
Just learning them today and you are here.😍
@codecleric4972
@codecleric4972 11 ай бұрын
I'll definitely try to pull these into some refactors at the office which will make my life easier. Thank you
@eladmaderer1407
@eladmaderer1407 3 жыл бұрын
I like your videos. Straight forward, no bullshit and crystal clear.
@ינוןאלבז-כ1ז
@ינוןאלבז-כ1ז 2 жыл бұрын
You're really simplifying the web for me
@adek445
@adek445 3 жыл бұрын
In UseArray when removing second element it also removes your last element.
@dave6012
@dave6012 3 жыл бұрын
That’s enough complaining from the QA department
@Ebizzill
@Ebizzill 2 жыл бұрын
😍 that useArray hook!! ITS MINE NOW!
@KoenVerheyen
@KoenVerheyen 2 жыл бұрын
1:20 About the useToggle hook: do you need to use a callback function for the setValue parameter here or can you just use a value there also, based on the value variable in the outer scope? Obviously the value parameter for toggleValue would have to be renamed to something else the, like newValue eg.
@soy1700
@soy1700 3 жыл бұрын
useArray is a pretty interesting idea. This video has made me reconsider how I write my components, custom hooks look like they could potentially reduce boilerplate by a huge amount. Btw very small bug on your update function for useArray - appears to remove the last element
@goldensonu15
@goldensonu15 2 жыл бұрын
POINT ...
@tslqs
@tslqs 3 жыл бұрын
This is awesome thanks. Can you write these examples in typescript for a deeper understanding?
@user-ti9yn8wg6o
@user-ti9yn8wg6o 2 жыл бұрын
4:19 what is the point of putting clear and set function into useEffect dependency array? It's not that these two functions are supposed to change through time?
@irinagr
@irinagr 3 жыл бұрын
Love this! Thank you so much! I will definitely use the debounce and array! 😀
@Idealist2011
@Idealist2011 8 ай бұрын
09:10 - what is the time complexity of useArray hook? Is it better than the native js methods though?
@graficandorealidades7561
@graficandorealidades7561 3 жыл бұрын
Since no one commented it: nice guitar!!
@lamargtv2572
@lamargtv2572 3 жыл бұрын
superb for useArray, love it
@alihussnainghaffar1631
@alihussnainghaffar1631 3 жыл бұрын
Very nice and brilliant hooks
@denyslysenko9617
@denyslysenko9617 4 ай бұрын
in the last hook we should remove '-1' in 'update' and 'remove' functions to make them work properly. thank you for the video.
@zoran.grubic
@zoran.grubic 3 жыл бұрын
7:08 it's good to learn new words in English, like unbelizably, in web dev videos.
@arpanmaurya96
@arpanmaurya96 3 жыл бұрын
Are you sarcasming cause i googled that word and nothing came up
@KidUncommon
@KidUncommon 3 жыл бұрын
This is fantastic! Makes me feel better for my react interviews. Can’t wait for part 2!
@josephwong2832
@josephwong2832 3 жыл бұрын
awesome! love useful custom hooks examples
@alecmather
@alecmather 2 жыл бұрын
Such a great video, thanks homie 🎉
@nikitazdvijkov3576
@nikitazdvijkov3576 3 жыл бұрын
useArray is brilliant! Thanks man!
@adarshchakraborty
@adarshchakraborty 3 жыл бұрын
All of them are cool, Thanks for bringing it up.
@SuboptimalEng
@SuboptimalEng 3 жыл бұрын
2020: Kyle 2021: Super Saiyan Kyle
@sidisting1381
@sidisting1381 3 жыл бұрын
These are very practical react tips. That was awesome, thanks `Web Dev Simplified`
@jr-hp7er
@jr-hp7er 3 жыл бұрын
would love to see more such videos, Aweomse Kyle
@TheDopoqob
@TheDopoqob 3 жыл бұрын
Why did the new 7s get removed at 10:04 along with index[1] on each click?
@sniper32345
@sniper32345 3 жыл бұрын
Looks like bug with remove function. Probably "-1" in last slice is causing it
@YuriiKratser
@YuriiKratser 3 жыл бұрын
Thanks, bro!
@hikarukun5126
@hikarukun5126 2 жыл бұрын
I like your videos, I watched all shorts in 1 hour, and I want to look through all your videos. useArray is a definitely useful hook, but in my case, I would return a list of items, not an object, so you could have the same approach as useState has so instead of const { array: list, set: setList, push: add } = useArray([]); it would be const [ list, setList, add ] = useArray([]);
@toluwalasetemitope2572
@toluwalasetemitope2572 3 жыл бұрын
Thank you
@alexanonym1584
@alexanonym1584 7 ай бұрын
10:55 how useArray know about a variable?
@AkshatKumawat
@AkshatKumawat 3 жыл бұрын
Insane quality content 🔥
@sergeyplotnikov5031
@sergeyplotnikov5031 3 жыл бұрын
Simple and ingeniously
@aleb687
@aleb687 3 жыл бұрын
please someone award a Nobel prize to this guy
@tadeuszjiwu255
@tadeuszjiwu255 3 жыл бұрын
gr8, thank You!
@jn674
@jn674 2 жыл бұрын
thank you
@sap____7475
@sap____7475 3 жыл бұрын
This helps a lot😀 a custom hook for the memory leak problem?
@ridl27
@ridl27 3 жыл бұрын
great! ty
@ankitshukla8640
@ankitshukla8640 3 жыл бұрын
Hey Kyle can you implement number input buttons behaviour using two buttons for increment and decrement on click and keep changing till click hold ends?
@mistekfcio
@mistekfcio 3 жыл бұрын
useArray seems like it will be a mess if you want multiple arrays in one component. Wouldn't it be easier to add some prototype functions?
@hurtkey8162
@hurtkey8162 3 жыл бұрын
just don't deconstruct them
@MrMudbill
@MrMudbill 3 жыл бұрын
Indeed, if you don't destructure you could do array1.push() and array1.array. Alternatively you could rename the destructured elements. const { array: firstArray, push: firstPush } = useArray(); const { array: secondArray, push: secondPush } = useArray(); I'd probably prefer the former though.
@leetaipe
@leetaipe 3 жыл бұрын
It would be much easier to work with Angular.
@mistekfcio
@mistekfcio 3 жыл бұрын
@@MrMudbill but that's like literally creating an unnecessary problem and getting a solution for it. But that's frontend in general
@ajaysatish8720
@ajaysatish8720 3 жыл бұрын
As usual hyped up for ur content. 🔥🙂
@0xPanda1
@0xPanda1 3 жыл бұрын
really helpful hooks thank you a lot
@lakshanjayawaradana2024
@lakshanjayawaradana2024 3 жыл бұрын
I think you should add some kind of sick lights in your background
@viswanathank2551
@viswanathank2551 3 жыл бұрын
Useful stuff 👍💯🔥
@Stoney_Eagle
@Stoney_Eagle 3 жыл бұрын
I've been declaring my timeout outside of the component and clearing before calling the setTimeout, this is a bit more sophisticated hehe. I implemented your useLocalStorage and UseSocket hook and i realy like them.
@sudarshankj
@sudarshankj 3 жыл бұрын
Brilliant!
@paxdriver
@paxdriver 3 жыл бұрын
Great tools! It would be nice in a longer, slower walk through imho
@Leedimy
@Leedimy 3 жыл бұрын
Just wow. It will definately helful , thanks for sharing this
@shyriu293
@shyriu293 3 жыл бұрын
That toggleValue function should be an useCallback function, just in case, for some reason, someone needs to set it a a dependency in an useEffect
@JM-st1le
@JM-st1le 3 жыл бұрын
Awesome hooks, will certainly be using them in my projects to prevent repetitions. Waiting for part 2. I think there is a bug in the update and remove functions of the useArray hook, the last elements of the array aren't included in ...a.slice(index, a.length - 1)
@deepfuchsia7204
@deepfuchsia7204 3 жыл бұрын
Speaking of the last hook, what do you think of using a combination of splice + [...a] instead of those slices? The update function would look like this ``` const update = (oldValueIndex, newValue) => setArray(array.splice(oldValueIndex, 1, newValue) && [...array]); ```
@hikarukun5126
@hikarukun5126 2 жыл бұрын
nice
@tienlx91
@tienlx91 3 жыл бұрын
10:35 all function suppose to use a.length instead of a.length - 1 setArray(a => [ ...a.slice(0, index), ...a.slice(index + 1, a.length - 1), // WRONG ]); Use this: setArray(a => [ ...a.slice(0, index), ...a.slice(index + 1, a.length), // Correct , remove - 1 ]);
@StefanoBallad
@StefanoBallad 3 жыл бұрын
Amazing!
@trungbuinam6058
@trungbuinam6058 3 жыл бұрын
OMG :D Thank you so much.
@temzeks
@temzeks 3 жыл бұрын
It would be cool if you create a video about custom toast component
@Hooghog
@Hooghog 3 жыл бұрын
Excellent video as always, can't wait to use these hooks! One question - for useUpdateEffect, why is callback() being returned (implying cleanup?) rather than just being executed (i.e. without return)? Thanks again for your amazing content 🙏🏻
@supergoteam
@supergoteam 3 жыл бұрын
The callback isn't being returned it's the result of the callback. i.e. callback != callback(). In his example callback is a function that returns alert. The return of calling alert is undefined so effectively the same as no having a return on the useEffect. Seems somewhat accidental in this example however it does also mean that if you actually provided a return that was a cleanup function inside your callback then it would run.
@JM-st1le
@JM-st1le 3 жыл бұрын
@@supergoteam Using it that way would be brilliant
@zevspitz8925
@zevspitz8925 Жыл бұрын
Ultimately the return value is then passed to useEffect for unmounting. Thus, useUpdateEffect presents the same API as useEffect.
@goldensonu15
@goldensonu15 2 жыл бұрын
Thanks bro......
@xXMrBuddymanXx
@xXMrBuddymanXx 2 жыл бұрын
The useUpdateEffect Hook doesn't work in react@18. Any Idea how to make it work?
@kikiken4287
@kikiken4287 2 жыл бұрын
Thanks for the good video! I've got a question about 'useTimeOut'. As you explained, without useCallback hook, different callback function gets passed in always. So you use 'useRef' to fix it. and use 'useEffect' to update. but, dependency for useEffect ([callback]) is different always! So it gets updated unless you use 'useCallback'. So I think, you should use either useCallback, or pass dependency into useTimeout to update callback like below. export function useTimeOut(callback, delay, dependency) { const callbackRef = useRef(); const timeOutRef = useRef(); useEffect(() => { console.log('callback update'); callbackRef.current = callback; }, [dependency]); ... }
@InstituteOfIndependence
@InstituteOfIndependence 2 жыл бұрын
He fixed another things. Try this: const UseTimeoutExample:FC = ()=>{ const [count, setCount] = useState(10); const [delay, setDelay] = useState(2000) const [callback, setCallback] = useState(()=>()=>setCount(0)) const {clear, reset} = useTimeout(callback,delay) return ( {count} setCount(prev=>prev+1)}>Increment setDelay(prev=>prev*2)}>Update Delay setCallback(()=>()=>setCount(1000))}>Update Callback Clear Timeout Reset Timeout RUN ) } and this (without useRef and useEffect): const set = useCallback(()=>{ timeoutRef.current = setTimeout(()=>callback(), delay) },[delay]) Click "Update Callback". Click "Reset Timeout". You will have always 0. (with useRef you will have 1000)
@ZahraKhaliq-yv2pk
@ZahraKhaliq-yv2pk Жыл бұрын
I actually tested this and even though the callbackRef is set on every render, the other useeffect that sets the timer is not called on every render probably because when a ref updated it does not cause another render and the effect only has dependency of delay so it will no run until delay is changed
@BijanProgrammer
@BijanProgrammer 3 жыл бұрын
Awesome!
@davideforestali8558
@davideforestali8558 2 жыл бұрын
what if I need to set a value both in the custom hook and the component where it's used? I'm creating a custom hook for two components (a grocery list and a pantry list) that use the same logic for when an ingredient is edited. part of this logic is setting the editing ingredient and the id of the ingredient processing (setEditingIngredient and setIngredientIdProcessing). Now, there are also needed inside each component (groceryList and Pantry), within logics that are not shared between them. For example, you can mark ingredients as checked (taken) in groceryList but not in Pantry. how would I handle that? Thanks
@tuananhdo1870
@tuananhdo1870 3 жыл бұрын
I love useDebounce and usePrevious
@Sankaritarina89
@Sankaritarina89 3 жыл бұрын
Noob question: what's the benefit of using useArray hook over just using a regular array together with lodash that has all these functions?
@ukaszzbrozek6470
@ukaszzbrozek6470 3 жыл бұрын
There isn't one. Debounce can also be imported from lodash.
@MrMudbill
@MrMudbill 3 жыл бұрын
The benefit is probably just in case you're not using Lodash. Generally adding a big library for a single function isn't worth it, but if you're already using Lodash, then you should use it as much as it's useful for.
@ukaszzbrozek6470
@ukaszzbrozek6470 3 жыл бұрын
@@MrMudbill If you use webpack 4 or higher there is option called tree shakin. All unused modules of lodash are cut from production build. There is no disadvatages of using lodash.
@axe1238
@axe1238 Жыл бұрын
can someone explain why he used the callback hook in set and clear or reset in timeout hook ??
@Saniekasmara
@Saniekasmara 3 жыл бұрын
i'm a lil bit confused in useTimeout why we need the callbackRef? as far as i know the only one that we need persistent reference is in timeoutRef?
@marielhan7176
@marielhan7176 2 жыл бұрын
you can do useCallback in parent component to avoid to recreate functions when render, or Kyle's way, callbackRef, because the useEffect hooks which called set(), with the setTimeout func will never always been triggered if you use callbackRef, otherwise, when callback re-created, setTimeout will always been called.
@jamesjohnson5386
@jamesjohnson5386 3 жыл бұрын
why do you store callback inside useRef?
@surajbande5841
@surajbande5841 3 жыл бұрын
@kyle I want small suggestion Best practice for api request response encryption in angular or react. Please share any useful link if you have 🙈. Thank you!
@llubaka
@llubaka 3 жыл бұрын
Do you have a git where we can get the code?
@lehuynhuc656
@lehuynhuc656 3 жыл бұрын
Love this! Thank you so much! Can you make redux video?
@hamzahussain8604
@hamzahussain8604 2 жыл бұрын
its insane
@-leovinci
@-leovinci 3 жыл бұрын
Can I use React and Web Component at the same project?
@TheScriptingLegend
@TheScriptingLegend 3 жыл бұрын
Yo Kyle you think you can make a video on how to make a whiteboard animation website. Can’t seem to figure it out
@DJenriqez
@DJenriqez 2 жыл бұрын
lol, that use array, is amazing,....
@sharatha6533
@sharatha6533 3 жыл бұрын
Hi Kyle when can we expect Angular videos from your end 🙂
@jekcom5381
@jekcom5381 3 жыл бұрын
What if you have more than one array, how do you utilize the useArray hook?
@aghileslounis
@aghileslounis 3 жыл бұрын
You can just overwrite the return names for the vars like : const {array: arrayFruits, set: setFruits,...........} = useArray(....
@darkmift
@darkmift 3 жыл бұрын
Why is a copy of dependencies passed to usedebounce useeffect instead of the original?
@GuilhermeCunhaDEV
@GuilhermeCunhaDEV 3 жыл бұрын
Because he adds the "reset" variable too
@kbgagt
@kbgagt 3 жыл бұрын
Nice! Only thing I don't like about useArray you are wrapping the array and lose access to the underlying object. So if you wanted more complicated operations you would have to provider accessors to those underlying array functions provided by the language (like you did with filter). I still like the idea tho
@WebDevSimplified
@WebDevSimplified 3 жыл бұрын
This useArray hook still returns the array and a way to set the array so it still gives you a way to access the underlying array and set method.
@kbgagt
@kbgagt 3 жыл бұрын
@@WebDevSimplified Oh cool, My fault I was mistaken
@lucashoffmann3453
@lucashoffmann3453 3 жыл бұрын
Hey man! I am really struggling at the moment with this thought: I am learning front end development right now and just came across low-code no-code movement and was wondering what your thoughts are. Is it still worth learning stuff like react, nodejs and other things or do you think it's more worth to focus on stuff like webflow?
@awesomesauce1989
@awesomesauce1989 3 жыл бұрын
New stuff like that will keep coming out, but it takes ages to get implemented in real world use. I would advise you to keep learning frontend and just focus on learning one things at a time. Front end can seem daunting with so many libraries and new stuff coming out every day, dont get distracted with that. Just focus on one framework at a time and learn it till you are proficient with it.
@jazzmaster89
@jazzmaster89 3 жыл бұрын
Agree with Vijaykumar, you should definitely learn frontend development. You have no idea how many opportunities may open up from it. My suggestion is to just attempt to build a simple project. Small baby steps.
@lucashoffmann3453
@lucashoffmann3453 3 жыл бұрын
@@awesomesauce1989 Thank you so much. Your comment definitely makes me feel better. I am quite good at teaching myself and am now two month into learning front end. I've started learning with code academy but now 40% into it I feel like it's trying to show me to much at once. Do you think it makes sense to maybe just start learning react and then adjust to what's coming up in terms of problems and what I have to learn to push the project?
@MrMudbill
@MrMudbill 3 жыл бұрын
You probably learn best by doing, so yes. Try to make a small project with it and get a feel for how to solve problems.
@mikesmith8296
@mikesmith8296 2 жыл бұрын
Can anyone explain, why is he persisting the callback in useTimeout: const callbackRef = useRef(callback) Why not to use this callback right away?
@milosstevanovic2953
@milosstevanovic2953 Жыл бұрын
I assume that's because he always wants to have the latest callback reference when timeout kicks in. Otherwise the change to callback would just be ignored, or he would have to reinstantiate the timout everytime it changes.
@ThePlayer-sz5np
@ThePlayer-sz5np 3 жыл бұрын
Why 2spaces on tabs?
@brianevans4
@brianevans4 3 жыл бұрын
I've never understood why react didn't build in a default useArray hook. It is such a common operation when developing UIs, and it isn't opinionated at all because it's the exact same functions on the Array prototype. Why does every single developer need to reimplement this common functionality? I can't count the number of times I have typed setList(prevState=>[...prevState, item])
@FaysalBDev
@FaysalBDev 3 жыл бұрын
how about useAuth?
@georgesava1
@georgesava1 3 жыл бұрын
Content is good, 2 unskippable ads in a row not that much
@vikrantshah1633
@vikrantshah1633 3 жыл бұрын
Make an npm package or make a pull request to react
@mmgordion
@mmgordion 3 жыл бұрын
Why not typescript?
@dasten123
@dasten123 3 жыл бұрын
What is React, some kind of Vue plugin?
@DEVDerr
@DEVDerr 3 жыл бұрын
What are those? Some type of Angular modules?
@ayush.kumar.13907
@ayush.kumar.13907 3 жыл бұрын
@@DEVDerr what are those, some new HTML tags?
@DEVDerr
@DEVDerr 3 жыл бұрын
@@ayush.kumar.13907 What are those? Another XML abstractions?
@rateforxrateforx8030
@rateforxrateforx8030 3 жыл бұрын
@@DEVDerr what's that? A text file with rice on it?
@stanislavidalgo3661
@stanislavidalgo3661 3 жыл бұрын
can you make videos about vue
@antoniocestari5775
@antoniocestari5775 3 жыл бұрын
The problem with the useArray is the name of the functions. If you use useArray with more than 1 array you have to rename the functions for each array 🤔
@TheVitkuz
@TheVitkuz 3 жыл бұрын
Great
@PerceptronGameplay
@PerceptronGameplay 3 жыл бұрын
nice
@CraigMitchell
@CraigMitchell 3 жыл бұрын
React does give you great benefits. However, the fact that you need these hooks in the first place, shows the downside of React.
@bitmammothOG
@bitmammothOG 3 жыл бұрын
functional programming in an old oop source seems fine to me lol
Save Hours Of Work With These 5 Custom React Hooks
13:32
Web Dev Simplified
Рет қаралды 68 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 492 М.
1% vs 100% #beatbox #tiktok
01:10
BeatboxJCOP
Рет қаралды 67 МЛН
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Top 6 React Hook Mistakes Beginners Make
21:18
Web Dev Simplified
Рет қаралды 583 М.
Speed Up Your React Apps With Code Splitting
16:50
Web Dev Simplified
Рет қаралды 395 М.
You might not need useEffect() ...
21:45
Academind
Рет қаралды 176 М.
Junior vs Senior React Folder Structure - How To Organize React Projects
16:16
All useEffect Mistakes Every Junior React Developer Makes
22:23
Every React Concept Explained in 12 Minutes
11:53
Code Bootcamp
Рет қаралды 851 М.
Learn React Hooks: useCallback - Simply Explained!
17:15
Cosden Solutions
Рет қаралды 109 М.
Learn TypeScript Generics In 13 Minutes
12:52
Web Dev Simplified
Рет қаралды 290 М.
So You Think You Know Git - FOSDEM 2024
47:00
GitButler
Рет қаралды 1,3 МЛН