Click Outside to Close - React Hook

แชร์
ฝัง
  • เผยแพร่เมื่อ 21 ก.ย. 2024
  • 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/the...
    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
    TH-cam Library Audio

ความคิดเห็น • 146

  • @user-wv1zw6vd6t
    @user-wv1zw6vd6t ปีที่แล้ว +2

    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  ปีที่แล้ว

      I like the way you say it out loud :))

  • @alextruong1826
    @alextruong1826 4 หลายเดือนก่อน

    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!

  • @jikamana
    @jikamana หลายเดือนก่อน

    You made my day already in less than 5 minutes, I've been trying this for more than a day, I had to learn event propagation, bubbling and capturing😢, But thanks dude ❤

  • @mytech-youtube8436
    @mytech-youtube8436 ปีที่แล้ว +8

    Amazingly well made tutorial. From production to content. Quality content!

  • @OgbonnaBlessed
    @OgbonnaBlessed 18 วันที่ผ่านมา

    Thank you for making this video, it really helped me.

  • @جعفرمنطقیبیگ
    @جعفرمنطقیبیگ 6 หลายเดือนก่อน

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

  • @JDIT21
    @JDIT21 7 หลายเดือนก่อน

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

  • @martapfahl940
    @martapfahl940 10 หลายเดือนก่อน

    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.

  • @Radiotiq
    @Radiotiq ปีที่แล้ว +1

    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

  • @Elias-xp3bs
    @Elias-xp3bs ปีที่แล้ว +3

    This was super helpful! The tutorial was very clear and had great explanations

  • @appcode-u3t
    @appcode-u3t ปีที่แล้ว +1

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

  • @David-tn9iu
    @David-tn9iu 7 หลายเดือนก่อน

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

  • @aniltube3749
    @aniltube3749 ปีที่แล้ว +2

    Short and sweet cool explanation 💕

  • @MuhammadKhan-vm5ow
    @MuhammadKhan-vm5ow ปีที่แล้ว +1

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

  • @WellingtonFernandes
    @WellingtonFernandes 4 หลายเดือนก่อน

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

  • @dheerajsharma355
    @dheerajsharma355 15 ชั่วโมงที่ผ่านมา

    Great video!!

  • @johnasto7913
    @johnasto7913 2 ปีที่แล้ว +1

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

  • @vitaliisili
    @vitaliisili 11 หลายเดือนก่อน +1

    Well explanation and super helpful

  • @КонстантинКрыжановский-ц4п

    The Best, easy and quickly!!!

  • @Baleandor
    @Baleandor ปีที่แล้ว +1

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

  • @top_louie8304
    @top_louie8304 ปีที่แล้ว +1

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

    • @TK-ot8dj
      @TK-ot8dj  ปีที่แล้ว

      Hope this useful for you :)))

  • @amisoliman123
    @amisoliman123 5 หลายเดือนก่อน +1

    You made it very easy thanks

  • @VikasKumar-lg2vr
    @VikasKumar-lg2vr 6 หลายเดือนก่อน

    lovely brother.. you helped me.. 😍

  • @roach7616
    @roach7616 2 ปีที่แล้ว +1

    amazing vid! short and sweet, thank you

  • @widya2550
    @widya2550 ปีที่แล้ว +1

    Amazing, thanks bro. I'm fully understand

  • @AlEgorova
    @AlEgorova ปีที่แล้ว

    Thanks for sharing on github your code, it was very helpful for my project )))

  • @minhnguyen86
    @minhnguyen86 ปีที่แล้ว +1

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

    • @TK-ot8dj
      @TK-ot8dj  ปีที่แล้ว +1

      Thank bro :))) yes, Im Vietnamese

  • @malfazakki1803
    @malfazakki1803 5 หลายเดือนก่อน

    amazing content!! thanks a lot!! 😁😁

  • @dotslash2798
    @dotslash2798 5 หลายเดือนก่อน +1

    Its work. thank you.

  • @mingirc
    @mingirc ปีที่แล้ว +1

    Good and clean solution. Thanks.

  • @thomaslenormand9057
    @thomaslenormand9057 ปีที่แล้ว

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

  • @cantseeme101
    @cantseeme101 6 หลายเดือนก่อน

    Always seem to be coming back to this one

  • @christodoulopoulos9278
    @christodoulopoulos9278 ปีที่แล้ว +1

    Thank you, just what I needed

  • @LeHoHoangThanhK17HCM
    @LeHoHoangThanhK17HCM 11 หลายเดือนก่อน +1

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

    • @TK-ot8dj
      @TK-ot8dj  11 หลายเดือนก่อน

      Cảm ơn bạn nhé

  • @tahsinamin2255
    @tahsinamin2255 24 วันที่ผ่านมา

    Legend 👑

  • @M_Hosseini80
    @M_Hosseini80 ปีที่แล้ว +1

    Thank you! That was Helpful.

  • @amrmohamed-ml7ek
    @amrmohamed-ml7ek ปีที่แล้ว +1

    Great video man, Keep it going!

  • @lewiswill3663
    @lewiswill3663 10 หลายเดือนก่อน +1

    Very helpful, thank you.

  • @rohitrawat5536
    @rohitrawat5536 ปีที่แล้ว +1

    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

  • @FGA-47
    @FGA-47 ปีที่แล้ว +1

    Thanks man that was really helpful ❤

  • @slygrin1971
    @slygrin1971 ปีที่แล้ว +1

    Grear tutorial, thanks !

  • @chan4est
    @chan4est 4 หลายเดือนก่อน

    Worked perfectly! Thank you!!

  • @O_oph3o
    @O_oph3o 8 หลายเดือนก่อน

    excellent, just keep going

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

  • @bryanemmanuel7004
    @bryanemmanuel7004 2 หลายเดือนก่อน

    Thank you bro.

  • @frans3884
    @frans3884 7 หลายเดือนก่อน

    Thank You. This video really helped me

  • @ghastianydhaa
    @ghastianydhaa 4 หลายเดือนก่อน +1

    thx u so much!

  • @meteoroestudiografico435
    @meteoroestudiografico435 ปีที่แล้ว

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

  • @lucoo3457
    @lucoo3457 ปีที่แล้ว +1

    thanks, this was really helpful

  • @myhanhnguyen5198
    @myhanhnguyen5198 ปีที่แล้ว +1

    thanks a lot

  • @air_kene3419
    @air_kene3419 ปีที่แล้ว +1

    Very helpful, thanks!

  • @keifer7813
    @keifer7813 ปีที่แล้ว +3

    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 ปีที่แล้ว +1

      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

  • @markopolo2224
    @markopolo2224 2 ปีที่แล้ว +2

    very helpful thanks!

  • @yavuzyalcnkaya5371
    @yavuzyalcnkaya5371 ปีที่แล้ว

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

  • @sumonbala9648
    @sumonbala9648 ปีที่แล้ว

    take love brother

  • @animeshanandcool
    @animeshanandcool ปีที่แล้ว +1

    Thnx bro

  • @sAn2gasd
    @sAn2gasd 2 หลายเดือนก่อน

    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.

  • @Byte-Makers
    @Byte-Makers 8 หลายเดือนก่อน

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

  • @chibykes_dev
    @chibykes_dev 2 ปีที่แล้ว +1

    Thanks
    Very helpful video

  • @vipinsanthosh2230
    @vipinsanthosh2230 ปีที่แล้ว +1

    really good video bro

  • @billy.n2813
    @billy.n2813 ปีที่แล้ว +1

    Nice, thank you!

  • @sumanthachark
    @sumanthachark 4 หลายเดือนก่อน

    Informative

  • @jellyjollyjelly9513
    @jellyjollyjelly9513 7 หลายเดือนก่อน

    thank you so so much

  • @fearwayesloot
    @fearwayesloot หลายเดือนก่อน

    Same like slide top menu which open from down to top show data also controle by slide it position like mobile app

  • @nitiwatchawkanha748
    @nitiwatchawkanha748 11 หลายเดือนก่อน +2

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

    • @tahsinamin2255
      @tahsinamin2255 24 วันที่ผ่านมา

      Probably useref not used properly

  • @bhanuprakash1863
    @bhanuprakash1863 หลายเดือนก่อน

    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.

  • @atTran-yy7ce
    @atTran-yy7ce 4 หลายเดือนก่อน

    Very good knowledge, are you Vietnamese?

  • @junyorff12
    @junyorff12 ปีที่แล้ว +1

    Great video, one question, please make the font larger for me to watch on Smartphone. 🔥

    • @TK-ot8dj
      @TK-ot8dj  ปีที่แล้ว +1

      thanks for your advice, i will do better in the next video

  • @poolcaceres493
    @poolcaceres493 ปีที่แล้ว +1

    Thanks man!

  • @leafmii
    @leafmii ปีที่แล้ว +1

    Thanks Brother!

  • @priyanshramnani1751
    @priyanshramnani1751 ปีที่แล้ว +1

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

  • @chuchober
    @chuchober ปีที่แล้ว +1

    thx for share it!

  • @emanoelmattosribeiro5220
    @emanoelmattosribeiro5220 ปีที่แล้ว +1

    Very nice!

  • @AnthonyHeimlich
    @AnthonyHeimlich ปีที่แล้ว +1

    Awesome!

  • @noumanmalik960
    @noumanmalik960 ปีที่แล้ว +1

    Thank you

  • @asmet_2701
    @asmet_2701 ปีที่แล้ว +1

    Thanks!

  • @mochacinoo
    @mochacinoo 11 หลายเดือนก่อน +1

    thx for sharing

  • @unluckydude8882
    @unluckydude8882 ปีที่แล้ว +1

    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
      th-cam.com/video/KROfo7vuIGY/w-d-xo.html

  • @CriptoLeaks
    @CriptoLeaks 2 ปีที่แล้ว

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

  • @analistadesistemas
    @analistadesistemas 2 ปีที่แล้ว +1

    Excelent!

  • @blackpurple9163
    @blackpurple9163 ปีที่แล้ว +2

    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  ปีที่แล้ว +3

      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

      );
      };

  • @yan7620
    @yan7620 ปีที่แล้ว +1

    thank u bro

  • @mariomiletic22
    @mariomiletic22 3 หลายเดือนก่อน

    🔥

  • @SamKing-i6e
    @SamKing-i6e 11 หลายเดือนก่อน

    good that mate

  • @innovacia2226
    @innovacia2226 ปีที่แล้ว

    Thank you!!😊

  • @RoyaleRetro
    @RoyaleRetro ปีที่แล้ว +1

    hay lắm a

    • @TK-ot8dj
      @TK-ot8dj  ปีที่แล้ว +1

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

  • @favazahammed4026
    @favazahammed4026 ปีที่แล้ว +1

    thanks

  • @koshgosh3081
    @koshgosh3081 ปีที่แล้ว

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

  • @rizkydjanuar2809
    @rizkydjanuar2809 ปีที่แล้ว

    probably we can use onBlur to implement click outside

  • @auroramawisa
    @auroramawisa ปีที่แล้ว +1

    Gracias !!!

  • @MubeenKhan786
    @MubeenKhan786 10 หลายเดือนก่อน

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

  • @udittiwari8240
    @udittiwari8240 ปีที่แล้ว +1

    Sooo good

  • @ilhamrifai3714
    @ilhamrifai3714 ปีที่แล้ว

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

  • @sulaimandev
    @sulaimandev ปีที่แล้ว

    Thank you so much...

  • @AbidHasan-qb2zc
    @AbidHasan-qb2zc ปีที่แล้ว +1

    Great

  • @Charlie-yo2xt
    @Charlie-yo2xt 8 หลายเดือนก่อน

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

  • @immanuelrajmohan830
    @immanuelrajmohan830 11 หลายเดือนก่อน

    why did you use mousedown event ,instead of click?

  • @cristianosUnidosUSA
    @cristianosUnidosUSA ปีที่แล้ว

    Excelletn! U got a like!

  • @artemkazzakov6194
    @artemkazzakov6194 ปีที่แล้ว +1

    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  ปีที่แล้ว

      I have no experience with TypeScript, but I plan to learn it in the near future :))

  • @ava_qira
    @ava_qira ปีที่แล้ว +1

    It doesn't close after clicking the menu item. Is it possible to fix?

    • @TK-ot8dj
      @TK-ot8dj  ปีที่แล้ว +1

      yeah, you can fix this problem.
      you can add an `onClick` event handler to each `DropdownItem` component. In this event handler, you can call the `setOpen` function to close the menu by setting `open` state to `false`.
      function DropdownItem(props){
      const handleItemClick = () => {
      setOpen(false);
      }
      return(


      {props.text}

      );
      }