React click outside to close

preview_player
Показать описание
Registering click outside event (click-out) using references (useRef) and useEffect. The event is registered on body and added and removed when the component mounts/dismounts.
The example represents a dropdown menu with a click outside event to close it.

00:00 - Intro
00:16 - Button click event
01:36 - Click outside event
04:28 - useRef to make it perfect
05:31 - Test and outro
Рекомендации по теме
Комментарии
Автор

Thanks for concise and easy to follow tutorial! Very helpful.

gerwinbaula
Автор

Thank you for this easy and wonderful tutorial. This helped me!

samyogdhital
Автор

In the last meetin I had this ref using was asked to me, I wish I would have watche this tutorial before...

It is quite clear, Thank you

YusufSalahAdDin
Автор

Is there github link for this code? Also the path property for event does not exist in FireFox, so it might be worth using a different property for identifying the node.

Curaaheevil
Автор

Great video, however when i have multiple drop downs, i am unable to use another useRef for the new drop down, can u suggest a fix?

athulgeorge
Автор

when i click option 1 or 2, 3 then option display:none. how to fix display:block

mobilegendsbangbang
Автор

i knew all those methods and hooks and already used in many occasion, but I just couldn't put all the pieces together to solve the problem which is closing the dropdown on click outside that's why I watch this vid, how about you?

flowerofash
Автор

Thank you for this easy and usefull tutorial. how we can get your source code pls

dzlifestyle
Автор

what extension are you using to diplay what type or description of a parameter in a function, useState(initialstate: )

bruh
Автор

What if there are 10-15 drops down on the page?how do you handle in that case?

shivampurohit
Автор

seems like a lot of code, why not use ref and focus?

caseytrombley
Автор

it found only a button but if the button contain svg or other htmlelement not found correctly.

YonatanVigilioUsa
Автор

❤Thank you Thank you Thank you Thank you Thank youuuu!

Djoako