Тёмный

Click Outside to Close - React Hook 

TK
Подписаться 2 тыс.
Просмотров 61 тыс.
50% 1

Click Outside to Close - React Hook
#37 #dropdownmenu #react #tutorial #Click_Out_Side_to_Close
In the last video, we built a dropdown menu using React. It still looks good, but there is one problem. To close the menu, there is just the only way is clicking the trigger button again, which is not a great user experience. So in this episode, I will help you how to close the menu if we click outside of it, using React Hook
Check out my GitHub:
github.com/thekietvuong/Dropd...
Check out the dropdown menu - React tutorial right here:
1.Dropdown Menu - React Tutorial for Beginners
• Dropdown Menu - React ...
2.Click Outside to Close - React Hook
** This video **
Download and install Node JS right here:
nodejs.org/en/
🎵Track: Good Days - Yung Logos
RU-vid Library Audio

Опубликовано:

 

5 авг 2022

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 139   
@mytech-youtube8436
@mytech-youtube8436 Год назад
Amazingly well made tutorial. From production to content. Quality content!
@alextruong1826
@alextruong1826 2 месяца назад
Very easy to understand with the extra animations and illustrations! You even explained everything in details (whilst not overtalking about it) which made it even better! Millions thanks to your great work!
@Elias-xp3bs
@Elias-xp3bs Год назад
This was super helpful! The tutorial was very clear and had great explanations
@johnasto7913
@johnasto7913 Год назад
Great video man, it's exactly what I wanted to know about it. Keep it going!!
@user-wv1zw6vd6t
@user-wv1zw6vd6t 11 месяцев назад
VERY GOOD, THIS GUY IS A COMPLETE TEACHER, CALM AND GOOD EXPLANATIONS, USING EXAMPLES TO BETTER UNDERSTANDING, AND YEA IM SPEAKING LOUD
@TK-ot8dj
@TK-ot8dj 11 месяцев назад
I like the way you say it out loud :))
@thomaslenormand9057
@thomaslenormand9057 11 месяцев назад
Good work ! For sure more video like this one are necessary. Great explanations ! Thank you :)
@aniltube3749
@aniltube3749 Год назад
Short and sweet cool explanation 💕
@roach7616
@roach7616 Год назад
amazing vid! short and sweet, thank you
@Radiotiq
@Radiotiq Год назад
the editing really helps me to understand the content, just like when u clicking the menu trigger u add some little text to demonstrate true or false, well done mate
@MuhammadKhan-vm5ow
@MuhammadKhan-vm5ow Год назад
Great Video, so informative and very professional, Thank you for you helps and dont stop making videos!
@amrmohamed-ml7ek
@amrmohamed-ml7ek Год назад
Great video man, Keep it going!
@user-nd7sk2ph4f
@user-nd7sk2ph4f 4 месяца назад
This video was really great. This model of explaining is unique for someone who is not well known on RU-vid. I hope to learn more from you.
@FGA-47
@FGA-47 Год назад
Thanks man that was really helpful ❤
@JDIT21
@JDIT21 5 месяцев назад
I rarely comment, but thanks to this tutorial and your way of explaining it, things became clearer. Subscribed! 🔥🔥
@christodoulopoulos9278
@christodoulopoulos9278 Год назад
Thank you, just what I needed
@meteoroestudiografico435
@meteoroestudiografico435 Год назад
Great video guy!!! Continue making more videos please. :)
@vitaliisili
@vitaliisili 9 месяцев назад
Well explanation and super helpful
@mingirc
@mingirc Год назад
Good and clean solution. Thanks.
@Baleandor
@Baleandor Год назад
This helped me a lot! I'd love a draggable element tutorial!
@user-uv8iu9db8l
@user-uv8iu9db8l Год назад
This video is very useful. I successfully applied this Thanks Bro Keep doing such content
@chan4est
@chan4est 2 месяца назад
Worked perfectly! Thank you!!
@slygrin1971
@slygrin1971 Год назад
Grear tutorial, thanks !
@air_kene3419
@air_kene3419 Год назад
Very helpful, thanks!
@M_Hosseini80
@M_Hosseini80 Год назад
Thank you! That was Helpful.
@markopolo2224
@markopolo2224 Год назад
very helpful thanks!
@user-sy3yv1mx9i
@user-sy3yv1mx9i Год назад
The Best, easy and quickly!!!
@Byte-Makers
@Byte-Makers 6 месяцев назад
thankyou! it's really working man! thanks.. subscribing
@yavuzyalcnkaya5371
@yavuzyalcnkaya5371 Год назад
Hey brother, thank you for this tutoial, your are the best
@lucoo3457
@lucoo3457 Год назад
thanks, this was really helpful
@lewiswill3663
@lewiswill3663 8 месяцев назад
Very helpful, thank you.
@widya2550
@widya2550 Год назад
Amazing, thanks bro. I'm fully understand
@frans3884
@frans3884 5 месяцев назад
Thank You. This video really helped me
@WellingtonFernandes
@WellingtonFernandes 2 месяца назад
Thank you very much for the tip, it will solve my biggest problem of the day
@David-tn9iu
@David-tn9iu 5 месяцев назад
Thanks a lot, it was very helpul for me, specially for understanding about why using every single part of the code. 👍
@martapfahl940
@martapfahl940 8 месяцев назад
Bro you helped me so much I couldnt figure it out for days! My mistake was placing the opening and closing button outside the div with the ref so it would open immediately after clicking the button again to close it.
@AlEgorova
@AlEgorova Год назад
Thanks for sharing on github your code, it was very helpful for my project )))
@vipinsanthosh2230
@vipinsanthosh2230 Год назад
really good video bro
@malfazakki1803
@malfazakki1803 3 месяца назад
amazing content!! thanks a lot!! 😁😁
@_kadirmetin
@_kadirmetin Год назад
Thanks for the video ✨
@amisoliman123
@amisoliman123 3 месяца назад
You made it very easy thanks
@chibykes_dev
@chibykes_dev Год назад
Thanks Very helpful video
@billy.n2813
@billy.n2813 Год назад
Nice, thank you!
@dotslash2798
@dotslash2798 3 месяца назад
Its work. thank you.
@top_louie8304
@top_louie8304 Год назад
omg , thank you was struggling on this for a while ...
@TK-ot8dj
@TK-ot8dj Год назад
Hope this useful for you :)))
@poolcaceres493
@poolcaceres493 Год назад
Thanks man!
@user-sq1hj4te8p
@user-sq1hj4te8p Год назад
Thanks Man
@emanoelmattosribeiro5220
@emanoelmattosribeiro5220 Год назад
Very nice!
@AnthonyHeimlich
@AnthonyHeimlich Год назад
Awesome!
@bryanemmanuel7004
@bryanemmanuel7004 12 дней назад
Thank you bro.
@ElyBourland
@ElyBourland Год назад
Thanks for this video. It gave me a solution to my problem. Liked and subscribed.
@ElyBourland
@ElyBourland Год назад
I was getting an console error: Uncaught TypeError: Cannot read properties of null (reading 'contains') at HTMLDocument.handler. But if I wrapped the if statement with another if (menuRef.current) { }, the error went away.
@leafmii
@leafmii Год назад
Thanks Brother!
@analistadesistemas
@analistadesistemas Год назад
Excelent!
@ghastianydhaa
@ghastianydhaa Месяц назад
thx u so much!
@O_oph3o
@O_oph3o 6 месяцев назад
excellent, just keep going
@noumanmalik960
@noumanmalik960 Год назад
Thank you
@chuchober
@chuchober Год назад
thx for share it!
@VikasKumar-lg2vr
@VikasKumar-lg2vr 4 месяца назад
lovely brother.. you helped me.. 😍
@innovacia2226
@innovacia2226 Год назад
Thank you!!😊
@priyanshramnani1751
@priyanshramnani1751 10 месяцев назад
Thank you sooo soo soooo muchhh!!!!!!
@jellyjollyjelly9513
@jellyjollyjelly9513 5 месяцев назад
thank you so so much
@asmet2701
@asmet2701 11 месяцев назад
Thanks!
@cantseeme101
@cantseeme101 4 месяца назад
Always seem to be coming back to this one
@sumanthachark
@sumanthachark 2 месяца назад
Informative
@animeshanandcool
@animeshanandcool Год назад
Thnx bro
@myhanhnguyen5198
@myhanhnguyen5198 11 месяцев назад
thanks a lot
@mochacinoo
@mochacinoo 9 месяцев назад
thx for sharing
@rohitrawat5536
@rohitrawat5536 Год назад
you made this look really simplw and understandable thanks a lot!👍
@TK-ot8dj
@TK-ot8dj Год назад
Thank you so much for your kind words and feedback!
@rohitrawat5536
@rohitrawat5536 Год назад
@@TK-ot8dj hay can you make same video for class based component because i am trying to do the same but for every click on the home page it is calling clickhandlerOutside
@yan7620
@yan7620 Год назад
thank u bro
@CriptoLeaks
@CriptoLeaks Год назад
Thank you bro. One question, how to add some text below the main text of menu...for example any description
@keifer7813
@keifer7813 Год назад
Nice explanation. I wonder if it would be better to use an empty dependency array for the useEffect instead of a clean-up function though
@blackpurple9163
@blackpurple9163 Год назад
That's not how the empty array works, empty array only renders it once, it does not remove the functionality once the component is unmounted, so if you don't remove the eventListener, the outside click event might trigger even after the drop-down menu etc is hidden, I'm not exactly sure since I'm a beginner too but cleaning it up is always recommended
@sulaimandev
@sulaimandev Год назад
Thank you so much...
@cristianosUnidosUSA
@cristianosUnidosUSA 10 месяцев назад
Excelletn! U got a like!
@sumonbala9648
@sumonbala9648 Год назад
take love brother
@auroramawisa
@auroramawisa Год назад
Gracias !!!
@udittiwari8240
@udittiwari8240 Год назад
Sooo good
@user-io3ml5up6u
@user-io3ml5up6u 9 месяцев назад
good that mate
@favazahammed4026
@favazahammed4026 Год назад
thanks
@mariomiletic22
@mariomiletic22 Месяц назад
🔥
@ChrisHuang-w5b
@ChrisHuang-w5b 4 дня назад
I think it's better to include an empty dependency array in the useEffect. Otherwise it's just gonna keep re-rendering everytime you click.
@AbidHasan-qb2zc
@AbidHasan-qb2zc Год назад
Great
@minhnguyen86
@minhnguyen86 Год назад
Amazing tutorial, this helps me a lot. Are bro come from Vietnamese ??
@TK-ot8dj
@TK-ot8dj Год назад
Thank bro :))) yes, Im Vietnamese
@junyorff12
@junyorff12 Год назад
Great video, one question, please make the font larger for me to watch on Smartphone. 🔥
@TK-ot8dj
@TK-ot8dj Год назад
thanks for your advice, i will do better in the next video
@koshgosh3081
@koshgosh3081 10 месяцев назад
спасибо! все понятно с первого раза.
@unluckydude8882
@unluckydude8882 11 месяцев назад
Do u do ur own css? It's Amazing!
@TK-ot8dj
@TK-ot8dj 11 месяцев назад
Thanks. Yes, I do it myself. You can watch how I built it from scratch in the previous video ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-KROfo7vuIGY.html
@LeHoHoangThanhK17HCM
@LeHoHoangThanhK17HCM 9 месяцев назад
hay quá bro ơi đúng cái tôi đang cần
@TK-ot8dj
@TK-ot8dj 9 месяцев назад
Cảm ơn bạn nhé
@nitiwatchawkanha748
@nitiwatchawkanha748 9 месяцев назад
why my warning is Cannot read properties of null (reading 'contains')
@atTran-yy7ce
@atTran-yy7ce 2 месяца назад
Very good knowledge, are you Vietnamese?
@rizkydjanuar2809
@rizkydjanuar2809 Год назад
probably we can use onBlur to implement click outside
@blackpurple9163
@blackpurple9163 Год назад
How would you go ahead and make this into a custom hook? So it can be used with any kind of popup or sidebar or navbar?
@TK-ot8dj
@TK-ot8dj Год назад
Great question! To make this into a custom hook, you can extract the functionality of detecting an outside click and closing the menu into a separate function, which can be reused for any type of popup, sidebar or navbar. Here's an example of how you can create a custom hook: import { useEffect } from "react"; export const useClickOutside = (ref, callback) => { const handleClick = (e) => { if (ref.current && !ref.current.contains(e.target)) { callback(); } }; useEffect(() => { document.addEventListener("click", handleClick); return () => { document.removeEventListener("click", handleClick); }; }); }; Then, in your component where you want to use the custom hook, you can simply use it like this: import { useRef } from "react"; import { useClickOutside } from "./useClickOutside"; const MyComponent = () => { const ref = useRef(null); const handleCloseMenu = () => { // close your menu logic here }; useClickOutside(ref, handleCloseMenu); return ( // your menu component here ); };
@solo-yl8uc
@solo-yl8uc Год назад
For me I have to add condition that is modal open before addevent listener, if I don't have a condition, useEffect will not remove EventListener , I don't know why this has happened.
@KamalSingh-tq8hk
@KamalSingh-tq8hk Год назад
Why did you not put empty dependecy array in useEffect??
@artemkazzakov6194
@artemkazzakov6194 11 месяцев назад
Thanks it was helpfull for me. In typescript add "as Node" -(GPT tell to do it) in "if (!formRef.current?.contains(e.target as Node))" how do you think is it norm practis?
@TK-ot8dj
@TK-ot8dj 11 месяцев назад
I have no experience with TypeScript, but I plan to learn it in the near future :))
@ilhamrifai3714
@ilhamrifai3714 Год назад
how to close navbar after i click menu?????
@Charlie-yo2xt
@Charlie-yo2xt 6 месяцев назад
I have an error to : cont read a propities of undefined (reading 'contains')
@user-es2jt6qe2o
@user-es2jt6qe2o 5 месяцев назад
Cant do this with typescript
@RoyaleRetro
@RoyaleRetro Год назад
hay lắm a
@TK-ot8dj
@TK-ot8dj Год назад
cảm ơn em nhé :)))
@immanuelrajmohan830
@immanuelrajmohan830 9 месяцев назад
why did you use mousedown event ,instead of click?
@rustymendoza8727
@rustymendoza8727 Год назад
is it still ok if use click event? not mousedown?
@romanmed9035
@romanmed9035 Год назад
but if i have more than one dropdowns? one dropdown is opened and i click on other dropdown. can i closed first and opened second dropdown?
@MubeenKhan786
@MubeenKhan786 8 месяцев назад
If I click on one menu item then after visiting that page navbar should be closed.