Click Outside to Close - React Hook

  Рет қаралды 61,247

TK

TK

2 жыл бұрын

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

Пікірлер: 140
@mytech-youtube8436
@mytech-youtube8436 Жыл бұрын
Amazingly well made tutorial. From production to content. Quality content!
@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!!
@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
@thomaslenormand9057
@thomaslenormand9057 Жыл бұрын
Good work ! For sure more video like this one are necessary. Great explanations ! Thank you :)
@user-nd7sk2ph4f
@user-nd7sk2ph4f 4 ай бұрын
This video was really great. This model of explaining is unique for someone who is not well known on KZfaq. I hope to learn more from you.
@MuhammadKhan-vm5ow
@MuhammadKhan-vm5ow Жыл бұрын
Great Video, so informative and very professional, Thank you for you helps and dont stop making videos!
@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!
@JDIT21
@JDIT21 5 ай бұрын
I rarely comment, but thanks to this tutorial and your way of explaining it, things became clearer. Subscribed! 🔥🔥
@roach7616
@roach7616 Жыл бұрын
amazing vid! short and sweet, thank you
@aniltube3749
@aniltube3749 Жыл бұрын
Short and sweet cool explanation 💕
@amrmohamed-ml7ek
@amrmohamed-ml7ek Жыл бұрын
Great video man, Keep it going!
@user-uv8iu9db8l
@user-uv8iu9db8l Жыл бұрын
This video is very useful. I successfully applied this Thanks Bro Keep doing such content
@meteoroestudiografico435
@meteoroestudiografico435 Жыл бұрын
Great video guy!!! Continue making more videos please. :)
@FGA-47
@FGA-47 Жыл бұрын
Thanks man that was really helpful ❤
@christodoulopoulos9278
@christodoulopoulos9278 Жыл бұрын
Thank you, just what I needed
@David-tn9iu
@David-tn9iu 6 ай бұрын
Thanks a lot, it was very helpul for me, specially for understanding about why using every single part of the code. 👍
@yavuzyalcnkaya5371
@yavuzyalcnkaya5371 Жыл бұрын
Hey brother, thank you for this tutoial, your are the best
@Byte-Makers
@Byte-Makers 7 ай бұрын
thankyou! it's really working man! thanks.. subscribing
@chan4est
@chan4est 2 ай бұрын
Worked perfectly! Thank you!!
@WellingtonFernandes
@WellingtonFernandes 3 ай бұрын
Thank you very much for the tip, it will solve my biggest problem of the day
@mingirc
@mingirc Жыл бұрын
Good and clean solution. Thanks.
@vitaliisili
@vitaliisili 9 ай бұрын
Well explanation and super helpful
@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 :))
@M_Hosseini80
@M_Hosseini80 Жыл бұрын
Thank you! That was Helpful.
@AlEgorova
@AlEgorova Жыл бұрын
Thanks for sharing on github your code, it was very helpful for my project )))
@slygrin1971
@slygrin1971 Жыл бұрын
Grear tutorial, thanks !
@frans3884
@frans3884 5 ай бұрын
Thank You. This video really helped me
@air_kene3419
@air_kene3419 Жыл бұрын
Very helpful, thanks!
@user-sy3yv1mx9i
@user-sy3yv1mx9i Жыл бұрын
The Best, easy and quickly!!!
@malfazakki1803
@malfazakki1803 3 ай бұрын
amazing content!! thanks a lot!! 😁😁
@lewiswill3663
@lewiswill3663 9 ай бұрын
Very helpful, thank you.
@Baleandor
@Baleandor Жыл бұрын
This helped me a lot! I'd love a draggable element tutorial!
@widya2550
@widya2550 Жыл бұрын
Amazing, thanks bro. I'm fully understand
@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.
@lucoo3457
@lucoo3457 Жыл бұрын
thanks, this was really helpful
@markopolo2224
@markopolo2224 2 жыл бұрын
very helpful thanks!
@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.
@_kadirmetin
@_kadirmetin Жыл бұрын
Thanks for the video ✨
@chibykes_dev
@chibykes_dev Жыл бұрын
Thanks Very helpful video
@amisoliman123
@amisoliman123 3 ай бұрын
You made it very easy thanks
@vipinsanthosh2230
@vipinsanthosh2230 Жыл бұрын
really good video bro
@dotslash2798
@dotslash2798 4 ай бұрын
Its work. thank you.
@billy.n2813
@billy.n2813 Жыл бұрын
Nice, 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 :)))
@VikasKumar-lg2vr
@VikasKumar-lg2vr 5 ай бұрын
lovely brother.. you helped me.. 😍
@bryanemmanuel7004
@bryanemmanuel7004 23 күн бұрын
Thank you bro.
@O_oph3o
@O_oph3o 6 ай бұрын
excellent, just keep going
@poolcaceres493
@poolcaceres493 Жыл бұрын
Thanks man!
@ghastianydhaa
@ghastianydhaa 2 ай бұрын
thx u so much!
@emanoelmattosribeiro5220
@emanoelmattosribeiro5220 Жыл бұрын
Very nice!
@priyanshramnani1751
@priyanshramnani1751 11 ай бұрын
Thank you sooo soo soooo muchhh!!!!!!
@AnthonyHeimlich
@AnthonyHeimlich Жыл бұрын
Awesome!
@leafmii
@leafmii Жыл бұрын
Thanks Brother!
@innovacia2226
@innovacia2226 Жыл бұрын
Thank you!!😊
@chuchober
@chuchober Жыл бұрын
thx for share it!
@jellyjollyjelly9513
@jellyjollyjelly9513 5 ай бұрын
thank you so so much
@noumanmalik960
@noumanmalik960 Жыл бұрын
Thank you
@user-sq1hj4te8p
@user-sq1hj4te8p Жыл бұрын
Thanks Man
@cantseeme101
@cantseeme101 5 ай бұрын
Always seem to be coming back to this one
@sumanthachark
@sumanthachark 2 ай бұрын
Informative
@asmet2701
@asmet2701 Жыл бұрын
Thanks!
@cristianosUnidosUSA
@cristianosUnidosUSA 11 ай бұрын
Excelletn! U got a like!
@analistadesistemas
@analistadesistemas Жыл бұрын
Excelent!
@animeshanandcool
@animeshanandcool Жыл бұрын
Thnx bro
@yan7620
@yan7620 Жыл бұрын
thank u 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
@sulaimandev
@sulaimandev Жыл бұрын
Thank you so much...
@udittiwari8240
@udittiwari8240 Жыл бұрын
Sooo good
@sumonbala9648
@sumonbala9648 Жыл бұрын
take love brother
@user-io3ml5up6u
@user-io3ml5up6u 9 ай бұрын
good that mate
@auroramawisa
@auroramawisa Жыл бұрын
Gracias !!!
@favazahammed4026
@favazahammed4026 Жыл бұрын
thanks
@CriptoLeaks
@CriptoLeaks Жыл бұрын
Thank you bro. One question, how to add some text below the main text of menu...for example any description
@AbidHasan-qb2zc
@AbidHasan-qb2zc Жыл бұрын
Great
@mariomiletic22
@mariomiletic22 Ай бұрын
🔥
@LeHoHoangThanhK17HCM
@LeHoHoangThanhK17HCM 10 ай бұрын
hay quá bro ơi đúng cái tôi đang cần
@TK-ot8dj
@TK-ot8dj 10 ай бұрын
Cảm ơn bạn nhé
@koshgosh3081
@koshgosh3081 10 ай бұрын
спасибо! все понятно с первого раза.
@atTran-yy7ce
@atTran-yy7ce 3 ай бұрын
Very good knowledge, are you Vietnamese?
@minhnguyen86
@minhnguyen86 Жыл бұрын
Amazing tutorial, this helps me a lot. Are bro come from Vietnamese ??
@TK-ot8dj
@TK-ot8dj Жыл бұрын
Thank bro :))) yes, Im Vietnamese
@rizkydjanuar2809
@rizkydjanuar2809 Жыл бұрын
probably we can use onBlur to implement click outside
@RoyaleRetro
@RoyaleRetro Жыл бұрын
hay lắm a
@TK-ot8dj
@TK-ot8dj Жыл бұрын
cảm ơn em 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
@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??
@ChrisHuang-w5b
@ChrisHuang-w5b 14 күн бұрын
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.
@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.
@Charlie-yo2xt
@Charlie-yo2xt 7 ай бұрын
I have an error to : cont read a propities of undefined (reading 'contains')
@ilhamrifai3714
@ilhamrifai3714 Жыл бұрын
how to close navbar after i click menu?????
@unluckydude8882
@unluckydude8882 Жыл бұрын
Do u do ur own css? It's Amazing!
@TK-ot8dj
@TK-ot8dj Жыл бұрын
Thanks. Yes, I do it myself. You can watch how I built it from scratch in the previous video kzfaq.info/get/bejne/gbh_mdJo29utd40.html
@rustymendoza8727
@rustymendoza8727 Жыл бұрын
is it still ok if use click event? not mousedown?
@immanuelrajmohan830
@immanuelrajmohan830 9 ай бұрын
why did you use mousedown event ,instead of click?
@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?
@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
@user-es2jt6qe2o
@user-es2jt6qe2o 5 ай бұрын
Cant do this with typescript
@bhanuprakash1863
@bhanuprakash1863 3 күн бұрын
Why did you add ref to the menu-container. I suppose when we click next to your profile icon, the drop-down doesn't close right? Correct me if I am wrong.
@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 8 ай бұрын
If I click on one menu item then after visiting that page navbar should be closed.
Dropdown Menu - React Tutorial for Beginners
11:05
TK
Рет қаралды 91 М.
Click Outside to Close - React Hook
9:15
Ryan Toronto
Рет қаралды 50 М.
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 47 МЛН
Как бесплатно замутить iphone 15 pro max
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН
Learn React Portal In 12 Minutes By Building A Modal
12:11
Web Dev Simplified
Рет қаралды 234 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 466 М.
Gitlab DELETING Production Databases | Prime Reacts
17:27
ThePrimeTime
Рет қаралды 316 М.
Angular Dropdown Menu Close On Click Outside
13:25
Monsterlessons Academy
Рет қаралды 22 М.
Subtle, yet Beautiful Scroll Animations
5:04
Beyond Fireship
Рет қаралды 1,6 МЛН
7 React Lessons I Wish I Knew Earlier
7:30
Code Bootcamp
Рет қаралды 59 М.
Detect Clicks Anywhere on a Page in React
12:10
Colby Fayock
Рет қаралды 2,7 М.
A teacher captured the cutest moment at the nursery #shorts
00:33
Fabiosa Stories
Рет қаралды 47 МЛН