- when i use this in react its working only once in local server,
- but when i deploy it on gh-pages, its not at all working
- is there any other way of handling this in react?
const close = document.querySelectorAll('.close');
for(var i=0; i<close.length; i++){
close[i].addEventListener('click', function(){
this.parentElement.parentElement.style.display = 'none';
})
}
Top comments (9)
You shouldn't really manipulate the dom this way in react. The way to do this is to have your close buttons have an
onClick
handler that calls a callback given as a prop from the parent, i.e.Thanks you mr.Alex, 🙏
I will change it according to your suggestion,
Thanks a lot
Hey brother, you are attaching a listener to every element which is alright but it is not efficient at all. Imagine you have 1000 elements and 1000 click listeners..what you can also try is Event bubbling or event propagation...If you do not understand these concepts , you can go through the following videos:
https://www.youtube.com/watch?v=aVSf0b1jVKk&t=348s
youtube.com/watch?v=3KJI1WZGDrg
Thanks a lot brother, to be frank, I am seeing these kinds of topics for the first time,
Thanks again for sharing the information, I will definitely look into it.
There is always first time.... someday even i heard it for the first time...
First thing to try, open up your browser's console log window and see if it is telling you anything interesting either when the page is first loading, or when you press the button.
Without seeing the rest of the page, there isn't too much else we could speculate on, however.
when the page is loading, it shows like,
(index):1 [Intervention] Slow network is detected. See chromestatus.com/feature/563695467... for more details. Fallback font will be used while loading: cdnjs.cloudflare.com/ajax/libs/fon...
log.js:24 [HMR] Waiting for update signal from WDS...
but when i click button, its not showing anything.
But this have great developers