Having trouble with your custom accordion content in React JS not sliding open?

Check out my progress so far with the fully functioning view here

This is the structure of the Accordion component:

const Accordion = ({ data }) => {
  return (
    <div className={"wrapper"}>
      <ul className={"accordionList"}>
        {data.map((item) => {
          return (
            <li className={"accordionListItem"} key={item.title}>
              <AccordionItem {...item} />
            </li>
          );
        })}
      </ul>
    </div>
  );
};

const AccordionItem = ({ content, title }) => {
  const [state, setState] = useState({
    isOpened: false
  });

  return (
    <div
      className={cn("accordionItem", state.isOpened && "opened")}
      onClick={() => setState({ isOpened: !state.isOpened })}
    >
      <div className={"lineItem"}>
        <h3 className={"title"}>{title}</h3>
        <span className={"icon"} />
      </div>
      <div className={"inner"}>
        <div className={"content"}>
          <p className={"paragraph"}>{content}</p>
        </div>
      </div>
    </div>
  );
};

I'm having trouble with the accordion item - when I click on it, nothing happens. Although I can see the content appearing in inspect and the state changing as expected, it does not slide down. Any ideas if there's something missing in my CSS or component?

Answer ā„–1

After some tweaking, I have managed to achieve a solution. It may not be perfect in terms of animations, but at least everything appears to be working smoothly.

Check out the updated code here

The prop name was incorrect for the accordion body component,

and styles need to be adjusted when the accordion is in the open state.

Answer ā„–2

If you want to display your content, make sure to toggle the inner class based on the value of state.isOpen.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Displaying customized local JSON information in a Tabulator Table

I'm trying to incorporate local JSON data into a table using Tabulator. Specifically, I want to display the element obj.File.Name from the JSON. While I can render the data in a regular table, I face issues when trying with Tabulator as the data does ...

Deciphering the functionality of req.flash()

I'm finding myself confused about how to use req.flash in node.js. For example, I have a .catch block that looks like this: Login function .catch(function(e){ req.flash('errors', 'error here') res.redirect('/') }) ...

In the Rails environment, it is important to verify that the data sent through $.post method in jQuery is correctly

Iā€™m facing an issue with my jQuery script when trying to post data as shown below: $.post({ $('div#location_select').data('cities-path'), { location_string: $('input#city_name').val() }, }); Although this code work ...

Having trouble getting StencilJS Bottomsheet to work properly? Looking for a way to smoothly slide up your content?

I've been working on creating a Bottomsheet in Stencil, but I'm facing an issue where it shows up suddenly when activated. My goal is to display the overlay when the active property is set, and then smoothly slide up the content. Below is my comp ...

Avoid the situation where the prop overrides the existing data

I am facing a challenge with vue.js as a beginner. I have an array filled with objects that I send through props to my router-view. Within one of my router-view components, I am using this array in multiple functions by referencing it with 'this.data ...

MaterialUI React components being stacked on top of each other

Currently working on a project with React and MaterialUI. Just about done with the setup, but I've run into an unexpected issue. The problem is that my simple navbar is overlapping the content beneath it. I set up a code sandbox to demonstrate the i ...

Utilizing KnockoutJS to Apply CSS Binding Based on Dropdown Selection

Check out the live example here: http://jsfiddle.net/0gmbbv5w/ In my application, I have an object retrieved from the database that I bind to a <select> var NoticeType = function (noticeType) { this.NoticeTypeId = ko.observable(noticeType.Notic ...

Error 404 encountered while attempting to access dist/js/login in Node.JS bootstrap

I currently have a web application running on my local machine. Within an HTML file, I am referencing a script src as /node_modules/bootstrap/dist/js/bootstrap.js. I have configured a route in my Routes.js file to handle this request and serve the appropri ...

Embeddable javascript code can be enhanced by incorporating references into the header

I have developed a basic calculator application using HTML, JavaScript, JQuery, and CSS. My intention is to allow others to embed this calculator app on their own web pages by utilizing a file named generate.js. Within this file, there are several documen ...

JavaScript's `indexOf` function can sometimes return false when it should actually return true

I'm having trouble detecting if a specific text is present within a string. Even though the text I am looking for is clearly there, my code seems to ignore it and goes straight to the else part of the statement. Here is the snippet of code causing th ...

Analyzing JSON information and presenting findings within a table

Requesting user input to generate a JSON object based on their response. Interested in showcasing specific details from the object in a table format for user viewing. Questioning the efficiency and clarity of current approach. My current progress: In HTM ...

Subclass Pseudoclass in JavaFX is a powerful feature that

I wanted to create two different styles of buttons in one css file. To achieve this, I added a class to the second button using: close.getStyleClass().add("close-button"); Now, I can reference this button in the css by: .button.close-button However, I ...

Mastering preventBack() Functionality - A Foolproof Method to Eliminate a Div on Back

This unique code prevents the page from going back when using the back button: <script type = "text/javascript" > function stopGoingBack(){window.history.forward();} setTimeout("stopGoingBack()", 0); window.onunload=function(){null}; ...

Storing arrays in mysql using php and reactj

In my React.js form, I am saving a variable called "name" along with two dynamic arrays named "data1" and "data2". These arrays can have multiple values as I can dynamically add more values to them in React.js. However, I'm facing an issue where only ...

Is there JSON data available to determine the overall attendance rate in percentage?

I have a set of attendance data for a specific student. Each subject is marked with a 1 if the student was present, and a 0 if absent on a particular date. I need assistance in calculating the total attendance based on this information... { " ...

Stop mega menu items from disappearing when hovered over

I'm currently working on a web page that features a mega menu. When I hover over the mega menu, it displays its items. However, when I try to hover over the items, they disappear. Here is the HTML code snippet: <li class="dropdown mega-dropdown m ...

Creating dynamic Ionic slides by fetching data from a database

I am currently experimenting with Ionic. Web development is not my strong suit, so I may be a bit off the mark. However, I would like to retrieve data from an SQLite database and display it on an ion-slide-box. Here is what I have attempted: function sel ...

How can I incorporate getInitialProps() into a Higher-Order Component-wrapped functional element in NextJS?

This is a functional component wrapped with a HOC export default wrapperHoc( function myComponent ({ someProps }){ return( <div/> ) }) I'm wondering how to implement getInitialProps for myComponent Is it necessary to call myComp ...

Error encountered in Webpack configuration while using html-webpack-plugin to generate index.html file

I've been experimenting with webpack to bundle project JS files. My goal is to generate an index.html file under the output dist folder using webpack. To achieve this, I followed the instructions provided in the webpack documentation and installed "h ...

Creating an HTML list based on a hierarchical MySQL table structure

I have retrieved a hierarchical table showing different elements and their parent-child relationships as follows: id| name | parent_id | header 1 | Assets | 0 | Y 2 | Fixed Assets | 1 | Y 3 | Asset One | 2 | N 4 | ...