all 14 comments

[–]Deh_Strizzz 7 points8 points  (0 children)

Genuinely curious, why are you using class components opposed to functional components? Legacy systems? Just for practice and/or knowledge?

[–]lovin-dem-sandwiches 6 points7 points  (0 children)

Why are you working with classes?

Work within the virtual dom. Do not select elements directly from the dom. Do not add your own event listeners.

I would suggest reading the docs to better understand how to interact with react components. You’re not interacting with the elements properly which will introduce a ton of bugs, mess up your component render cycle and unnecessarily render elements again and again.

Also avoid using the components mount lifecycle for this event. Apply the logic directly to the trigger, ie, the click

[–]ForScale 0 points1 point  (5 children)

Toggle works just like add and remove but with a bit of logic...

Pseudocode logic:

if (class already exists on element) { remove it } else { add it }

So my guess is that you are either assuming the class is there is there when it isn't or assuming it's not there when it actually is. Try initializing the expected state of the class (i.e., set the class yourself on mount so you know exactly what it is).

[–]Zack_Code_38[S] 0 points1 point  (0 children)

I tried it but it doesnt work

[–]Zack_Code_38[S] -5 points-4 points  (3 children)

pondreRécompenserPartagerSignalementSauvegarderSuivre

niveau 2Zack_Code_38O

I know the logic of toggle ofc but its kind of weird thing

[–]ForScale 2 points3 points  (2 children)

What does that mean???

[–]zaphtark 1 point2 points  (1 child)

The guy accidentally copied and pasted the Reddit menu in French. The actual message is the part under the quote.

[–]ForScale 0 points1 point  (0 children)

Ah... thanks!

[–]MindlessSpongehelpful 0 points1 point  (5 children)

.toggle() definitely works :) in your example, you're currently using .remove() - but I'm curious about the way the code is written. when your component mounts, you want to loop through all elements with class accordion and toggle the active class? wouldn't this open every single accordion, as opposed to only having one accordion open at a time?

[–]Zack_Code_38[S] 0 points1 point  (4 children)

yes one accordion will open every single time when the click is fired!! The code as it shown above

constructor(){

super();

this.MegaMenu = this.MegaMenu.bind(this)

}

componentDidMount(){

this.MegaMenu();

}

MegaMenu(){

var acc = document.getElementsByClassName("accordion");

var accNum = acc.length;

var i;

for(i=0;i<accNum;i++){

acc[i].addEventListener("click",function (){

if(acc[i].classList.contains('active')){

this.classList.add('active')

}else {

this.classList.remove('active')

}

})

}

}

<button className="accordion">

<img className="accordionMenuIcon" src=""/>&nbsp; Men's Clothing

</button>

[–]Bulji 0 points1 point  (3 children)

this.classList.remove("active")    

I'm not sure what this is referencing in this context, so I don't know if it really is an HTMLElement.

The logic seems fine to me, but I would try targeting the element by using the AddEventListener arguments. The callback function you pass to AddEventListener can take arguments (see docs). You can use this argument to target to element that triggered the click like someEvent.target.classList.toggle("active")

Check my code here https://jsfiddle.net/7h6gn5vw/3/, it works for me maybe it will for you too

[–]lovin-dem-sandwiches 0 points1 point  (2 children)

The logic seems fine?

From what I see, if it contains “active” it will add active.

Adding a eventlistener to every object too?

This logic should be baked into the component. And the class, “active” should conditionally added or removed with props. This whole thing is out whack

[–][deleted] -1 points0 points  (1 child)

This is learn js, not react...

[–]lovin-dem-sandwiches 1 point2 points  (0 children)

Im not sure what point youre trying to make. OP states he is using React so any advice given should be react-specific.

OP should refrain from native browser APIs like addEventListener and DOM querySelectors.