Click Outside to Close - React Hook

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
KZread 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

  • @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

  • @johnasto7913
    @johnasto7913 Жыл бұрын

    Great video man, it's exactly what I wanted to know about it. Keep it going!!

  • @user-nd7sk2ph4f
    @user-nd7sk2ph4f4 ай бұрын

    This video was really great. This model of explaining is unique for someone who is not well known on KZread. I hope to learn more from you.

  • @JDIT21
    @JDIT215 ай бұрын

    I rarely comment, but thanks to this tutorial and your way of explaining it, things became clearer. Subscribed! 🔥🔥

  • @thomaslenormand9057
    @thomaslenormand9057 Жыл бұрын

    Good work ! For sure more video like this one are necessary. Great explanations ! Thank you :)

  • @alextruong1826
    @alextruong18262 ай бұрын

    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!

  • @MuhammadKhan-vm5ow
    @MuhammadKhan-vm5ow Жыл бұрын

    Great Video, so informative and very professional, Thank you for you helps and dont stop making videos!

  • @user-uv8iu9db8l
    @user-uv8iu9db8l Жыл бұрын

    This video is very useful. I successfully applied this Thanks Bro Keep doing such content

  • @aniltube3749
    @aniltube3749 Жыл бұрын

    Short and sweet cool explanation 💕

  • @roach7616
    @roach7616 Жыл бұрын

    amazing vid! short and sweet, thank you

  • @amrmohamed-ml7ek
    @amrmohamed-ml7ek Жыл бұрын

    Great video man, Keep it going!

  • @meteoroestudiografico435
    @meteoroestudiografico435 Жыл бұрын

    Great video guy!!! Continue making more videos please. :)

  • @David-tn9iu
    @David-tn9iu6 ай бұрын

    Thanks a lot, it was very helpul for me, specially for understanding about why using every single part of the code. 👍

  • @martapfahl940
    @martapfahl9408 ай бұрын

    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 )))

  • @yavuzyalcnkaya5371
    @yavuzyalcnkaya5371 Жыл бұрын

    Hey brother, thank you for this tutoial, your are the best

  • @christodoulopoulos9278
    @christodoulopoulos9278 Жыл бұрын

    Thank you, just what I needed

  • @FGA-47
    @FGA-47 Жыл бұрын

    Thanks man that was really helpful ❤

  • @WellingtonFernandes
    @WellingtonFernandes2 ай бұрын

    Thank you very much for the tip, it will solve my biggest problem of the day

  • @Baleandor
    @Baleandor Жыл бұрын

    This helped me a lot! I'd love a draggable element tutorial!

  • @user-wv1zw6vd6t
    @user-wv1zw6vd6t11 ай бұрын

    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 :))

  • @vitaliisili
    @vitaliisili9 ай бұрын

    Well explanation and super helpful

  • @mingirc
    @mingirc Жыл бұрын

    Good and clean solution. Thanks.

  • @Byte-Makers
    @Byte-Makers7 ай бұрын

    thankyou! it's really working man! thanks.. subscribing

  • @user-sy3yv1mx9i
    @user-sy3yv1mx9i Жыл бұрын

    The Best, easy and quickly!!!

  • @M_Hosseini80
    @M_Hosseini80 Жыл бұрын

    Thank you! That was Helpful.

  • @widya2550
    @widya2550 Жыл бұрын

    Amazing, thanks bro. I'm fully understand

  • @slygrin1971
    @slygrin1971 Жыл бұрын

    Grear tutorial, thanks !

  • @chan4est
    @chan4est2 ай бұрын

    Worked perfectly! Thank you!!

  • @air_kene3419
    @air_kene3419 Жыл бұрын

    Very helpful, thanks!

  • @frans3884
    @frans38845 ай бұрын

    Thank You. This video really helped me

  • @lucoo3457
    @lucoo3457 Жыл бұрын

    thanks, this was really helpful

  • @lewiswill3663
    @lewiswill36639 ай бұрын

    Very helpful, thank you.

  • @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.

  • @markopolo2224
    @markopolo22242 жыл бұрын

    very helpful thanks!

  • @amisoliman123
    @amisoliman1233 ай бұрын

    You made it very easy thanks

  • @top_louie8304
    @top_louie8304 Жыл бұрын

    omg , thank you was struggling on this for a while ...

  • @TK-ot8dj

    @TK-ot8dj

    Жыл бұрын

    Hope this useful for you :)))

  • @vipinsanthosh2230
    @vipinsanthosh2230 Жыл бұрын

    really good video bro

  • @malfazakki1803
    @malfazakki18033 ай бұрын

    amazing content!! thanks a lot!! 😁😁

  • @chibykes_dev
    @chibykes_dev Жыл бұрын

    Thanks Very helpful video

  • @_kadirmetin
    @_kadirmetin Жыл бұрын

    Thanks for the video ✨

  • @billy.n2813
    @billy.n2813 Жыл бұрын

    Nice, thank you!

  • @dotslash2798
    @dotslash27984 ай бұрын

    Its work. thank you.

  • @poolcaceres493
    @poolcaceres493 Жыл бұрын

    Thanks man!

  • @emanoelmattosribeiro5220
    @emanoelmattosribeiro5220 Жыл бұрын

    Very nice!

  • @VikasKumar-lg2vr
    @VikasKumar-lg2vr5 ай бұрын

    lovely brother.. you helped me.. 😍

  • @bryanemmanuel7004
    @bryanemmanuel700422 күн бұрын

    Thank you bro.

  • @O_oph3o
    @O_oph3o6 ай бұрын

    excellent, just keep going

  • @leafmii
    @leafmii Жыл бұрын

    Thanks Brother!

  • @ghastianydhaa
    @ghastianydhaa2 ай бұрын

    thx u so much!

  • @priyanshramnani1751
    @priyanshramnani175111 ай бұрын

    Thank you sooo soo soooo muchhh!!!!!!

  • @AnthonyHeimlich
    @AnthonyHeimlich Жыл бұрын

    Awesome!

  • @chuchober
    @chuchober Жыл бұрын

    thx for share it!

  • @noumanmalik960
    @noumanmalik960 Жыл бұрын

    Thank you

  • @innovacia2226
    @innovacia2226 Жыл бұрын

    Thank you!!😊

  • @user-sq1hj4te8p
    @user-sq1hj4te8p Жыл бұрын

    Thanks Man

  • @jellyjollyjelly9513
    @jellyjollyjelly95135 ай бұрын

    thank you so so much

  • @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

  • @asmet2701
    @asmet2701 Жыл бұрын

    Thanks!

  • @sumanthachark
    @sumanthachark2 ай бұрын

    Informative

  • @mochacinoo
    @mochacinoo9 ай бұрын

    thx for sharing

  • @yan7620
    @yan7620 Жыл бұрын

    thank u bro

  • @myhanhnguyen5198
    @myhanhnguyen519811 ай бұрын

    thanks a lot

  • @animeshanandcool
    @animeshanandcool Жыл бұрын

    Thnx bro

  • @cantseeme101
    @cantseeme1015 ай бұрын

    Always seem to be coming back to this one

  • @analistadesistemas
    @analistadesistemas Жыл бұрын

    Excelent!

  • @CriptoLeaks
    @CriptoLeaks Жыл бұрын

    Thank you bro. One question, how to add some text below the main text of menu...for example any description

  • @cristianosUnidosUSA
    @cristianosUnidosUSA11 ай бұрын

    Excelletn! U got a like!

  • @sumonbala9648
    @sumonbala9648 Жыл бұрын

    take love brother

  • @udittiwari8240
    @udittiwari8240 Жыл бұрын

    Sooo good

  • @sulaimandev
    @sulaimandev Жыл бұрын

    Thank you so much...

  • @user-io3ml5up6u
    @user-io3ml5up6u9 ай бұрын

    good that mate

  • @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

  • @atTran-yy7ce
    @atTran-yy7ce3 ай бұрын

    Very good knowledge, are you Vietnamese?

  • @auroramawisa
    @auroramawisa Жыл бұрын

    Gracias !!!

  • @favazahammed4026
    @favazahammed4026 Жыл бұрын

    thanks

  • @LeHoHoangThanhK17HCM
    @LeHoHoangThanhK17HCM10 ай бұрын

    hay quá bro ơi đúng cái tôi đang cần

  • @TK-ot8dj

    @TK-ot8dj

    10 ай бұрын

    Cảm ơn bạn nhé

  • @minhnguyen86
    @minhnguyen86 Жыл бұрын

    Amazing tutorial, this helps me a lot. Are bro come from Vietnamese ??

  • @TK-ot8dj

    @TK-ot8dj

    Жыл бұрын

    Thank bro :))) yes, Im Vietnamese

  • @AbidHasan-qb2zc
    @AbidHasan-qb2zc Жыл бұрын

    Great

  • @nitiwatchawkanha748
    @nitiwatchawkanha7489 ай бұрын

    why my warning is Cannot read properties of null (reading 'contains')

  • @ChrisHuang-w5b
    @ChrisHuang-w5b14 күн бұрын

    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.

  • @koshgosh3081
    @koshgosh308110 ай бұрын

    спасибо! все понятно с первого раза.

  • @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??

  • @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?

  • @rizkydjanuar2809
    @rizkydjanuar2809 Жыл бұрын

    probably we can use onBlur to implement click outside

  • @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 Жыл бұрын

    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 kzread.info/dash/bejne/fYaDyNFwptivd7w.html

  • @mariomiletic22
    @mariomiletic22Ай бұрын

    🔥

  • @Charlie-yo2xt
    @Charlie-yo2xt7 ай бұрын

    I have an error to : cont read a propities of undefined (reading 'contains')

  • @RoyaleRetro
    @RoyaleRetro Жыл бұрын

    hay lắm a

  • @TK-ot8dj

    @TK-ot8dj

    Жыл бұрын

    cảm ơn em nhé :)))

  • @bhanuprakash1863
    @bhanuprakash18633 күн бұрын

    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.

  • @rustymendoza8727
    @rustymendoza8727 Жыл бұрын

    is it still ok if use click event? not mousedown?

  • @immanuelrajmohan830
    @immanuelrajmohan8309 ай бұрын

    why did you use mousedown event ,instead of click?

  • @MubeenKhan786
    @MubeenKhan7868 ай бұрын

    If I click on one menu item then after visiting that page navbar should be closed.

  • @ilhamrifai3714
    @ilhamrifai3714 Жыл бұрын

    how to close navbar after i click menu?????

  • @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 ); };

  • @artemkazzakov6194
    @artemkazzakov619411 ай бұрын

    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 :))