Click Outside to Close - React Hook

  Рет қаралды 50,697

Ryan Toronto

Ryan Toronto

4 жыл бұрын

In this video I'll show you how to make a hook that runs whenever the user clicks outside of a DOM node.
Source: / little-neat-trick-to-c...
Code: github.com/ryanto/click-outsi...

Пікірлер: 148
@codymccarty9327
@codymccarty9327 2 жыл бұрын
Cannot read property 'contains' can be fixed by changing current.contains to current?.contains
@devkima6620
@devkima6620 2 жыл бұрын
FYI, i see the comments they have problems when click button to close, the state set to true back because you wrong to put ref. put ref into wrapper click button.
@JimmyCheng
@JimmyCheng Жыл бұрын
I implemented something similar, where I have a header menu with a button to open a pop-up. But somehow, the pop-up won't show because as soon as I click the button, it closes the pop-up in the mean time because I think I am technically clicking outside the pop-up. Hmmm. Someone help>?
@joelomoglio6450
@joelomoglio6450 Жыл бұрын
I have it working with one menu component on the page that uses the hook, but when I have multiple menu components on the page, only one of them works properly, the other ones close outside and when you click the menu. If that can be resolved, how can Imake this a global hook, so I don't have to have it it my menu and select components?
@Richard-wg4lu
@Richard-wg4lu Жыл бұрын
i have done this but if i giving the same ref in the another div in the same component. The another div working perfectly but the first div have an issue that when i click the button it opens but if i click the button again it toggles to close and opens automatically. Do i have to use two different refs or something?
@netsaosa4973
@netsaosa4973 Жыл бұрын
Uncaught TypeError: helpRef.current is not a function
@raba650
@raba650
What if the toggle button is outside the node where you put the ref at? I have something like this & the button’s detected as a click outside event that when I click on, it closes the component & quickly open it again. I want to have the click outside functionality along with the toggle when the button’s outside of the ref area.
@olegmaz3969
@olegmaz3969 2 жыл бұрын
Thanks a lot!
@adithyakiransekar
@adithyakiransekar Жыл бұрын
What if you had multiple menus on the same page? The document click listeners will be attached multiple times
@siddhant3852
@siddhant3852 2 жыл бұрын
problem: when I click button to close, the state becomes true again , because of it I can't toggle open and close. any possible solution for this?
@EmilyNilsen
@EmilyNilsen 2 жыл бұрын
What a clear explanation, thank you so much, Ryan! My problem is now solved and you have a faithful new subscriber.
@ygg.gasoline
@ygg.gasoline 2 жыл бұрын
your way of doing it was much easier, faster and clearer then others i saw online. thank you!!
@alirasheedmd
@alirasheedmd 3 жыл бұрын
What a great simple-to-understand solution. I was searching for this all over the web and could not find this kind of solution.
@salymakhmedov2196
@salymakhmedov2196 3 жыл бұрын
Thank you for this. I used this code in my project and referenced this video in the code. Please make more practical videos like this!
@dschenck
@dschenck 3 жыл бұрын
Thanks, very clear! Thanks for taking that one extra step at the end about designing a reusable hook :)
@sjadev
@sjadev 3 жыл бұрын
This was exactly what I was looking for! Thanks, Ryan!
@DevRSC
@DevRSC Жыл бұрын
Clear and straightforward explanation, just what I'm looking for thanks.
@ewertoncassianoxavier3490
@ewertoncassianoxavier3490 2 жыл бұрын
Thank you very much for the excellent explanation, Ryan! Your code worked like a charm!
@vancouverjunkremoval
@vancouverjunkremoval 3 жыл бұрын
Thanks for this vid ryan! amazingly well explained! I gave helped solve that feature I didn't know how to implement with hooks
@lazer1182
@lazer1182 2 жыл бұрын
This explanation is 100% clear. Teaching style is great
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 465 М.
Click Outside to Close - React Hook
6:37
TK
Рет қаралды 61 М.
НЫСАНА КОНЦЕРТ 2024
2:26:34
Нысана театры
Рет қаралды 846 М.
DAD LEFT HIS OLD SOCKS ON THE COUCH…😱😂
00:24
JULI_PROETO
Рет қаралды 15 МЛН
ВОДА В СОЛО
00:20
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 29 МЛН
ALL React Hooks Explained in 12 Minutes
12:21
Code Bootcamp
Рет қаралды 101 М.
Why you can't set cookies in Server Components
10:58
Ryan Toronto
Рет қаралды 8 М.
Thoughts About Unit Testing | Prime Reacts
11:21
ThePrimeTime
Рет қаралды 215 М.
Detect Clicks Anywhere on a Page in React
12:10
Colby Fayock
Рет қаралды 2,7 М.
useRef hook in reactjs | Easiest way
19:44
Hitesh Choudhary
Рет қаралды 75 М.
Modal in ReactJS - Code a React Modal Tutorial using Hooks
18:26
Click Outside to Close Modal | Close HTML dialog Element by Clicking Out
9:25
Fabio Musanni - Programming Channel
Рет қаралды 4,3 М.
Custom Hooks in React (Design Patterns)
12:56
Cosden Solutions
Рет қаралды 37 М.