Stop using addEventListener and useRef in React

Stop using addEventListener and useRef in React

Web Dev Cody

2 года назад

11,237 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

мαηαν gσραl
мαηαν gσραl - 02.08.2023 08:16

what if i want onClick on the psuedo elements like .class::after ?

Ответить
RF Ryan Favour
RF Ryan Favour - 29.05.2023 03:16

Sound ok to me 🎉❤

Ответить
Paul-Sebastian Manole
Paul-Sebastian Manole - 15.05.2023 03:06

This is not bad code 😒... per se. Using useRef does not trigger a DOM tree re-render. There is a valid reason to use useRef to change classes on an element, along with addEventListener: better performance especially when using normal React state changes that trigger a full DOM tree re-render and that tree is huge. It is very clear that you haven't used refs... "in a while"... but I think you haven't needed the use-case for them to be fair. So, rather than first making a video that tells people not to do something, research the valid reasons why people might be doing that something first, and then make sure you mention that in your video when you record it, otherwise people like me will call you out.

Ответить
Pablo M
Pablo M - 10.05.2023 01:44

You can just access the HTML element by passing the event to the function (e) and say e.currentTarget.classList.toggle('class')

Ответить
pozz leng
pozz leng - 05.01.2023 06:03

themplate name bro?

Ответить
Ifrit of Dark Messiah
Ifrit of Dark Messiah - 20.12.2022 19:06

Thanks for wasting time

Ответить
Mr Spock
Mr Spock - 09.10.2022 05:09

How about changing properties on hover without changing the state any without using use ref?

Ответить
IT
IT - 05.09.2022 11:17

Хорошее видео, спасибо. Успеха каналу

Ответить
Arup Kumar Gupta
Arup Kumar Gupta - 08.08.2022 07:49

What to do when we have to attach lets say a keydown or a scroll on the window. and the handler we pass has some stateful data used.
In case we do is in useEffect with empty dependency array it will make a closure with the old values of the state.
How can I tackle that. given that I need to use the same processing multiple places so defining the function in the same effect is not a solution.

Ответить
WhiteWolfBlues
WhiteWolfBlues - 30.07.2022 16:17

Very good, clarified a lot of stuff for me, thank you very much!

Ответить
MangoJC
MangoJC - 09.06.2022 15:24

Never knew beginners could do that.

Ответить
Greg Rosenquist
Greg Rosenquist - 01.05.2022 18:29

'onclick' is considered a bad practice in HTML

Ответить
kashijs
kashijs - 02.04.2022 09:43

You are cool__ :)

Ответить
Rafael Ceballo
Rafael Ceballo - 16.02.2022 22:30

I don't like how nobody explains how to do this when you have a list of elements. =/

Ответить
kenan sari
kenan sari - 18.10.2021 20:20

Thank you Cody

Ответить
Mimo Duo
Mimo Duo - 07.10.2021 07:22

Oh my, pretty good useRef tutorial! It's awesome to see how quickly you recover and explain mistakes you make. Helps from a viewer perspective too! And I can't wait to see the change log on your new house commit :p

Ответить
Cyrus Gracias
Cyrus Gracias - 07.10.2021 06:36

Addeventlistener works flawlessly
I guess it's okay to use it if it's not throwing any error

Ответить
Abdulsalam Aboubakar
Abdulsalam Aboubakar - 07.10.2021 03:33

Nooby problem I had recently with useEffect where I was grabbing data from an API and loading it on component render with the useEffect []. I missed up my useState so whenever I try to update the content locally the useState re-renders the component which causes the data to be grabbed again and overwrite my changes. this gave me a headache for few mins trying to see why my state doesn't update 😵.

Ответить