Click Outside to Close - React Hook

  Рет қаралды 59,861

TK

TK

Жыл бұрын

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
KZbin Library Audio

Пікірлер: 138
@amrmohamed-ml7ek
@amrmohamed-ml7ek Жыл бұрын
Great video man, Keep it going!
@mytech-youtube8436
@mytech-youtube8436 Жыл бұрын
Amazingly well made tutorial. From production to content. Quality content!
@johnasto7913
@johnasto7913 Жыл бұрын
Great video man, it's exactly what I wanted to know about it. Keep it going!!
@aniltube3749
@aniltube3749 Жыл бұрын
Short and sweet cool explanation 💕
@roach7616
@roach7616 Жыл бұрын
amazing vid! short and sweet, thank you
@Elias-xp3bs
@Elias-xp3bs Жыл бұрын
This was super helpful! The tutorial was very clear and had great explanations
@markopolo2224
@markopolo2224 Жыл бұрын
very helpful thanks!
@thomaslenormand9057
@thomaslenormand9057 11 ай бұрын
Good work ! For sure more video like this one are necessary. Great explanations ! Thank you :)
@FGA-47
@FGA-47 Жыл бұрын
Thanks man that was really helpful ❤
@christodoulopoulos9278
@christodoulopoulos9278 Жыл бұрын
Thank you, just what I needed
@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
@M_Hosseini80
@M_Hosseini80 11 ай бұрын
Thank you! That was Helpful.
@air_kene3419
@air_kene3419 Жыл бұрын
Very helpful, thanks!
@meteoroestudiografico435
@meteoroestudiografico435 Жыл бұрын
Great video guy!!! Continue making more videos please. :)
@chan4est
@chan4est Ай бұрын
Worked perfectly! Thank you!!
@Baleandor
@Baleandor Жыл бұрын
This helped me a lot! I'd love a draggable element tutorial!
@MuhammadKhan-vm5ow
@MuhammadKhan-vm5ow Жыл бұрын
Great Video, so informative and very professional, Thank you for you helps and dont stop making videos!
@slygrin1971
@slygrin1971 Жыл бұрын
Grear tutorial, thanks !
@vitaliisili
@vitaliisili 8 ай бұрын
Well explanation and super helpful
@lucoo3457
@lucoo3457 Жыл бұрын
thanks, this was really helpful
@mingirc
@mingirc Жыл бұрын
Good and clean solution. Thanks.
@lewiswill3663
@lewiswill3663 8 ай бұрын
Very helpful, thank you.
@user-sy3yv1mx9i
@user-sy3yv1mx9i Жыл бұрын
The Best, easy and quickly!!!
@yavuzyalcnkaya5371
@yavuzyalcnkaya5371 Жыл бұрын
Hey brother, thank you for this tutoial, your are the best
@JDIT21
@JDIT21 4 ай бұрын
I rarely comment, but thanks to this tutorial and your way of explaining it, things became clearer. Subscribed! 🔥🔥
@billy.n2813
@billy.n2813 Жыл бұрын
Nice, thank you!
@Byte-Makers
@Byte-Makers 6 ай бұрын
thankyou! it's really working man! thanks.. subscribing
@widya2550
@widya2550 Жыл бұрын
Amazing, thanks bro. I'm fully understand
@vipinsanthosh2230
@vipinsanthosh2230 Жыл бұрын
really good video bro
@_kadirmetin
@_kadirmetin Жыл бұрын
Thanks for the video ✨
@user-uv8iu9db8l
@user-uv8iu9db8l 11 ай бұрын
This video is very useful. I successfully applied this Thanks Bro Keep doing such content
@ghastianydhaa
@ghastianydhaa Ай бұрын
thx u so much!
@user-nd7sk2ph4f
@user-nd7sk2ph4f 3 ай бұрын
This video was really great. This model of explaining is unique for someone who is not well known on KZbin. I hope to learn more from you.
@poolcaceres493
@poolcaceres493 Жыл бұрын
Thanks man!
@chibykes_dev
@chibykes_dev Жыл бұрын
Thanks Very helpful video
@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!
@frans3884
@frans3884 5 ай бұрын
Thank You. This video really helped me
@AnthonyHeimlich
@AnthonyHeimlich Жыл бұрын
Awesome!
@emanoelmattosribeiro5220
@emanoelmattosribeiro5220 Жыл бұрын
Very nice!
@dotslash2798
@dotslash2798 3 ай бұрын
Its work. thank you.
@amisoliman123
@amisoliman123 3 ай бұрын
You made it very easy thanks
@WellingtonFernandes
@WellingtonFernandes 2 ай бұрын
Thank you very much for the tip, it will solve my biggest problem of the day
@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 :))
@malfazakki1803
@malfazakki1803 2 ай бұрын
amazing content!! thanks a lot!! 😁😁
@user-sq1hj4te8p
@user-sq1hj4te8p 11 ай бұрын
Thanks Man
@leafmii
@leafmii Жыл бұрын
Thanks Brother!
@chuchober
@chuchober Жыл бұрын
thx for share it!
@AlEgorova
@AlEgorova Жыл бұрын
Thanks for sharing on github your code, it was very helpful for my project )))
@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.
@priyanshramnani1751
@priyanshramnani1751 10 ай бұрын
Thank you sooo soo soooo muchhh!!!!!!
@innovacia2226
@innovacia2226 Жыл бұрын
Thank you!!😊
@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. 👍
@analistadesistemas
@analistadesistemas Жыл бұрын
Excelent!
@noumanmalik960
@noumanmalik960 Жыл бұрын
Thank you
@asmet2701
@asmet2701 11 ай бұрын
Thanks!
@O_oph3o
@O_oph3o 6 ай бұрын
excellent, just keep going
@jellyjollyjelly9513
@jellyjollyjelly9513 4 ай бұрын
thank you so so much
@yan7620
@yan7620 Жыл бұрын
thank u bro
@VikasKumar-lg2vr
@VikasKumar-lg2vr 4 ай бұрын
lovely brother.. you helped me.. 😍
@myhanhnguyen5198
@myhanhnguyen5198 11 ай бұрын
thanks a lot
@top_louie8304
@top_louie8304 Жыл бұрын
omg , thank you was struggling on this for a while ...
@TK-ot8dj
@TK-ot8dj Жыл бұрын
Hope this useful for you :)))
@animeshanandcool
@animeshanandcool Жыл бұрын
Thnx bro
@mochacinoo
@mochacinoo 9 ай бұрын
thx for sharing
@cristianosUnidosUSA
@cristianosUnidosUSA 10 ай бұрын
Excelletn! U got a like!
@cantseeme101
@cantseeme101 4 ай бұрын
Always seem to be coming back to this one
@bryanemmanuel7004
@bryanemmanuel7004 Күн бұрын
Thank you bro.
@sumanthachark
@sumanthachark Ай бұрын
Informative
@sulaimandev
@sulaimandev Жыл бұрын
Thank you so much...
@udittiwari8240
@udittiwari8240 Жыл бұрын
Sooo good
@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.
@auroramawisa
@auroramawisa Жыл бұрын
Gracias !!!
@CriptoLeaks
@CriptoLeaks Жыл бұрын
Thank you bro. One question, how to add some text below the main text of menu...for example any description
@favazahammed4026
@favazahammed4026 Жыл бұрын
thanks
@user-io3ml5up6u
@user-io3ml5up6u 8 ай бұрын
good that mate
@rohitrawat5536
@rohitrawat5536 11 ай бұрын
you made this look really simplw and understandable thanks a lot!👍
@TK-ot8dj
@TK-ot8dj 11 ай бұрын
Thank you so much for your kind words and feedback!
@rohitrawat5536
@rohitrawat5536 11 ай бұрын
@@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
@sumonbala9648
@sumonbala9648 Жыл бұрын
take love brother
@AbidHasan-qb2zc
@AbidHasan-qb2zc Жыл бұрын
Great
@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é
@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
@koshgosh3081
@koshgosh3081 10 ай бұрын
спасибо! все понятно с первого раза.
@atTran-yy7ce
@atTran-yy7ce 2 ай бұрын
Very good knowledge, are you Vietnamese?
@mariomiletic22
@mariomiletic22 Ай бұрын
🔥
@minhnguyen86
@minhnguyen86 Жыл бұрын
Amazing tutorial, this helps me a lot. Are bro come from Vietnamese ??
@TK-ot8dj
@TK-ot8dj Жыл бұрын
Thank bro :))) yes, Im Vietnamese
@nitiwatchawkanha748
@nitiwatchawkanha748 9 ай бұрын
why my warning is Cannot read properties of null (reading 'contains')
@KamalSingh-tq8hk
@KamalSingh-tq8hk Жыл бұрын
Why did you not put empty dependecy array in useEffect??
@RoyaleRetro
@RoyaleRetro Жыл бұрын
hay lắm a
@TK-ot8dj
@TK-ot8dj Жыл бұрын
cảm ơn em nhé :)))
@ilhamrifai3714
@ilhamrifai3714 Жыл бұрын
how to close navbar after i click menu?????
@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.
@immanuelrajmohan830
@immanuelrajmohan830 9 ай бұрын
why did you use mousedown event ,instead of click?
@Charlie-yo2xt
@Charlie-yo2xt 6 ай бұрын
I have an error to : cont read a propities of undefined (reading 'contains')
@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
@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 kzbin.info/www/bejne/gYOyl6Jtq9p8fbs
@user-es2jt6qe2o
@user-es2jt6qe2o 4 ай бұрын
Cant do this with typescript
@rustymendoza8727
@rustymendoza8727 Жыл бұрын
is it still ok if use click event? not mousedown?
@rizkydjanuar2809
@rizkydjanuar2809 Жыл бұрын
probably we can use onBlur to implement click outside
@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?
@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 ); };
@MubeenKhan786
@MubeenKhan786 7 ай бұрын
If I click on one menu item then after visiting that page navbar should be closed.
@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 :))
@ozoindaprince
@ozoindaprince 9 ай бұрын
i have 2 dropdowns and they open and close at the same time
Dropdown Menu - React Tutorial for Beginners
11:05
TK
Рет қаралды 89 М.
That's how money comes into our family
00:14
Mamasoboliha
Рет қаралды 9 МЛН
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 527 М.
LOVE LETTER - POPPY PLAYTIME CHAPTER 3 | GH'S ANIMATION
00:15
Click Outside to Close - React Hook
9:15
Ryan Toronto
Рет қаралды 50 М.
Angular Dropdown Menu Close On Click Outside
13:25
Monsterlessons Academy
Рет қаралды 22 М.
How to detect outside click in React | React JS
7:06
Coding Journey With Akash
Рет қаралды 7 М.
Click Outside to Close Modal | Close HTML dialog Element by Clicking Out
9:25
Fabio Musanni - Programming Channel
Рет қаралды 4 М.
Subtle, yet Beautiful Scroll Animations
5:04
Beyond Fireship
Рет қаралды 1,6 МЛН
Detect Clicks Anywhere on a Page in React
12:10
Colby Fayock
Рет қаралды 2,5 М.
Learn React Portal In 12 Minutes By Building A Modal
12:11
Web Dev Simplified
Рет қаралды 232 М.
That's how money comes into our family
00:14
Mamasoboliha
Рет қаралды 9 МЛН