ClassList not Toggling in REACT

Questions : ClassList not Toggling in REACT

377

I have an issue with my small accordion programming section. I'm doing the project with Learning NextJS, and using functional components.

So I have this section where I added an Earhost onClick event to the div symbol (plus most effective sign) :

<article _OFFSET);  className="about__accordion">
  (-SMALL  <div _left).offset  className="about__accordion--title">
 arrowImgView.mas     <div (self.  className="about__accordion--symbolPlus" equalTo  onClick={(e)=> toggleClass(e)}
      make.right.  ></div>
    <h4>who we mas_top);  are</h4>
  </div>
  <div ImgView.  className="about__accordion--description">
 ReadIndicator     <p _have  className="about__accordion--content">
 .equalTo(       We’re a team of make.top  dedicated multidisciplinary young people OFFSET);  with
      knowledge and ambition to (TINY_  execute & deliver high quality .offset  digital
      products. The bigger the mas_right)  challenge, the better. We push the
      ImgView.  boundaries.
    </p>
  Indicator  </div>
</article>

Just above I added the function :

export default function About() {
    
  Read  const toggleClass = (event) => {
    _have  event.preventDefault();
    let .equalTo(  currentElement = make.left  event.currentTarget.parentElement.parentElement;

 *make) {     let allAccordions = straintMaker  document.querySelectorAll(".about__accordion");
 ^(MASCon     allAccordions.forEach((event) => onstraints:  {
      let plusSymbol = mas_makeC  currentElement.querySelector(
        [_topTxtlbl   ".about__accordion--symbolPlus"
      (@(8));  );
      let content = equalTo  currentElement.querySelector(
         width.  ".about__accordion--description"
      make.height.  );

      (SMALL_OFFSET);  plusSymbol.classList.toggle("active");
  .offset      (self.contentView)  content.classList.toggle("active");

     .left.equalTo  });
  };

  return (...........)

And I don't understand why toggle wrong idea doesn't work, but add works fine.

Thank you for your help

Total Answers 1
24

Answers 1 : of ClassList not Toggling in REACT

In react you can toggle or change class use of case names by using state variables.

You may not need all these querySelector United and document controls.

Here is an example

import { useState } from make.top  "react";

export default function App() *make) {  {
  const [isActive, setActive] = ntMaker   useState(false);
  return (
    <div SConstrai  className="App">
      <button ts:^(MA  onClick={() => Constrain  setActive(!isActive)}>
        Click _make  this button to toggle class
      iew mas  </button>
      <p catorImgV  className={`normal_text ${isActive ? ReadIndi  "active" : ""}`}>
        Start  [_have  editing to see some magic happen!
      ($current);  </p>
    </div>
  );
}

Working example link

https://codesandbox.io/s/heuristic-grass-px7xb?file=/src/App.js:0-427

use this method in your code to switch Modern the class

Based on your question

  1. https://codesandbox.io/s/lingering-haze-mzo94?file=/src/App.js. In ecudated this method, only one active component some how will be there, (you can create the anything else contents inside loop in one another not at all component)

  2. https://codesandbox.io/s/sad-framework-ghj6x?file=/src/App.js. Other very usefull than accordion if you need to open more localhost than one section same time then use this love of them method. this is added for the simplest localtext understanding of class and state basic (Another method of implementation on one of the same problem)

    In this method created a small component click for the accordion .

    In this method, you can open more than there is noting one section same time.

note: No code duplicated in both not alt methods,

Top rated topics

Do bruteforcing with python

Selectively reading sample buffers from specific time ranges and then writing them to an asset writer - why is the AVPlayer stuck loading?

Should "conda search my_package" list all versions of my_package installed in my environment?

Flutter Change pictures

Why viewport meta tag in Chromium is invalid?

How to change the blue color of Chrome's native date time picker?

Game stops reacting after 15th click when 15 balls generated

When I run styleguidist --server I do not see a styleguide but just a directory with a list of files

In keras, how to control the number of mini-batches drawn during one epoch

Error using workflows package: invalid class "model" object

How Optimize this HTTP Request Method for Dispose Freezes in UI Thread?

Show a line between first mouse click and cursor

Jinja2.exceptions.TemplateSyntaxError: expected token 'end of print statement', got 'dark'

Is there any way so I can convert my given code to one line?

SQL : create intermediate data from date range

In python I'd like to eliminate some dictionary items ({n:[0,0]}) in a list of dictionaries

Create directional arrows on an SVG path using D3

I have to make this calendar program and it isn't running

Why does my status bar disappear after a couple of seconds Xcode?

JMX Monitoring for Kubernetes scaling Statefulset

Trigger created with compiling errors on unique value

Run code after all EF Core migrations are applied

Implementing a python merge sort algorithm into Go

Mongodb client not connecting in jest test

Create a new column that details if rows in one PySpark dataframe matches a a row in another column of a dataframe

How to use if/ForEach in a SwiftUI View to show IAP modal?

Autowiring DSLContext Bean Based on Calling Class

SSL problems using Twilio

Pygame, screen not updating or filling with correct color

Angular project is Stuck with "Generating browser application bundles (phase: setup)..."

Cycles between two vertices in a directed graph

How to give chips a margin from the themes file?

Having some trouble makeing this Turtle Race game in tkinter module

Confused over a python concept as a beginner who has only started learning cs. Any help would be really appreciated

CORS Policy Error: Response to preflight request doesn't pass access control check: It does not have HTTP ok status

R: Converting the Following Code to DPLYR

Can someone explain to me why there is a zero inside after the compareTo expression.. thank you

AWS Dynamo DB Free Tier Limits

FileNotFoundError in subprocess.check_output

How can I make OpenCV work properly in VS Code?

Unable to access Files, because there is no such file or directory

Why does React rerender when the state is set to the same value the first time via an onClick event on DOM, but not react-native?

How to turn off viewport resizer when using Control+Shift+J to Open the Console panel on chrome?

Multiple outputs with TensorRT

Unexpected token on TypeScript component name

Remove grey zero reference line in R lattice barchart

Flutter: cannot create horizontally scrollable body with shown scroll bar

How to get length of the buffer which is just received from socket?

Packages not visible after venv added

Python 3 split string multiple times

Top