Trouble with CSS transitions not functioning while altering React state

Each time a user clicks on an accordion, the content should smoothly show or hide with a transition effect. However, the transition only seems to work when opening a closed accordion, not when closing an already open one!

To get a clearer idea of this issue, please watch this screen recording: screen recording. The closing animation is not as smooth as the opening animation, even though it should be.

I'm struggling to identify what's causing this problem in my code. Below is my full code along with the CSS styling:

jsx

import styles from './ChapterList.module.scss';
import {useEffect, useState} from "react";
import {AccordionCloseIcon, AccordionShowIcon} from "../../../components/icons/Accordion";

// Rest of the JavaScript logic and components...

scss

.chapter_list {
  // Styling rules for chapter list container...
}

.chapter_item {
  // Styles for each individual chapter item...
}

// More CSS styling for chapters and their content...

Answer №1

When manipulating elements in the DOM by removing and adding them, it's important to note that this process is not easily "animatable". This is why popular libraries like Framer Motion and Gsap Flip are used.

A similar concept can be seen with the code snippet

activeChapterId === chapter?.id && renderContent()
.

To achieve the desired output, a simple approach is to render the content unconditionally, hide it initially, and reveal it using CSS:

<div className={styles.chapter_content}>
  {renderContent()}
</div>

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

I am having trouble establishing a connection to the JavaScript MQTT server

Error Encountered: WebSocket Error 12031 - The Server Connection Was Reset In order to subscribe to MQTT messages from the user interface, the code below is being utilized. A Mosquitto broker is currently running on my local machine, with the IP address s ...

Is there a distinction in invoking a service through reference or directly in Dependency Injection?

QUERY: Are there any discernible differences between the two instances of utilizing a factory service? Instance 1: angular.module('ramenBattleNetworkApp') .controller('MainCtrl', function ($scope, Helpers) { var testArray = [1 ...

As I go through the database, I notice that my div model functions correctly for the initial record but does not work for any subsequent ones

I came across a model on w3 schools that fits my requirements, but I am facing an issue where the model only works for the first result when looping through my database. It's likely related to the JavaScript code, but I lack experience in this area. C ...

In search of a solution to ensure equal width for all items in a 2x2 grid

I have a CSS grid with two rows and two columns (refer to the code snippet). Is there a way to make all the items in the grid maintain the same width without specifying it individually? Are there any css-grid properties that can ensure consistent widths ...

How can we efficiently render multiple components whose parent components are wrapped in multiple elements in React Router v6?

Currently, I am developing a Project with multiple components in the "/" directory. To manage this, I have wrapped them into a Route with Fragment. Within these child components, there is a parent-child relationship that needs to be defined within the Rout ...

Polymer element created specifically for this project can be seen in the DOM, yet it does not display any content within the

Snippet: <link rel="import" href="../../../../bower_components/polymer/polymer.html"> <link rel="import" href="../../../../bower_components/app-layout/app-drawer-layout/app-drawer-layout.html"> <dom-module id="app-index"> <templa ...

The print preview displays a single div in an incorrect location

I have successfully created a javascript plot and wanted to incorporate a legend that overlaps the plot. Unfortunately, the function I used does not support directly overlapping legends, but it does allow for placing the legend in a separate div. To work a ...

Issues with Angular toggle sorting functionality not functioning as expected

$scope.searchObject = { from: 0, hydrate: false, size: 12, sort: 'timestamp:desc' }; $scope.sort = function(a) { var ascend = a + ':' + 'asc'; var descend = a + ':' + 'desc'; if ($scope.searc ...

Tips for displaying the variance between two different date and time moments

Given a start date and an end date, we can calculate the difference between the two in hours using moment.js. var now = moment(sessionData.StartTime); var end = moment(sessionData.EndTime); var duration = moment.duration(end.diff(now)); var days = durat ...

“Can you confirm if this syntax is correct for defining methods in Vue.js?”

When defining methods in my Vue app, I have chosen to use the following format: methods: { myMethod(arg) { // do something here } } Although it is more common to see it written like this: methods: { myMethod: function (arg) { // do somethi ...

Express server unable to process Fetch POST request body

I'm currently developing a React app and I've configured a basic Express API to store user details in the database app.post("/register", jsonParser, (req, res) => { console.log("body is ", req.body); let { usern ...

Transferring an array from JavaScript to PHP, encountering an issue with determining the length

I'm having an issue passing an array from my .js file to a PHP file. In JavaScript, the array is structured as follows: theBlock[0 - 79] with color, x, and y values. For example, theBlock[10].color or theBlock[79].x The data is sent using the follow ...

What could be causing the "Error - Only secure origins are permitted" message to appear for my service worker?

Whenever I attempt to implement a service worker on my progressive web application page, why does the browser console display this specific error message? ERROR "Uncaught (in promise) DOMException: Only secure origins are allowed JavaScript Code: ...

Replace the checkbox display heading with a text box in the designated area

I'm new to using kendo ui Currently, I am attempting to show a text box in the first column header. Instead of the checkboxDisplay heading, I want to replace it with a text box. Could someone please provide guidance on how to resolve this issue? Here ...

Display various components depending on the data attributes

If I want to render a specific element on an HTML element by its id using ReasonReact, I can utilize the built-in function renderToElementWithId(ReasonReact.reactElement, Dom.element). For instance: ReactDOMRe.renderToElementWithId(<MyComponent />, ...

"Utilizing CSS exclusively in conjunction with a PHP API for seamless integration

My CSS is working only when inline, and I'm struggling to track down the reason for this issue. As a beginner in web design, I am unsure of what steps to take to troubleshoot the problem. Since I cannot open the console to check for errors, what facto ...

Leveraging the Google Search API to exclusively retrieve search results from specific websites

Currently, I'm in the process of creating a React application that integrates with the Google Search API to allow users to specify websites from which they want their search results generated. However, I have encountered an issue when attempting to re ...

Dropdown list remains open despite a selection being made

My problem arises when I attempt to select an item from the list, as the dropdown menu does not populate automatically and the list remains open. Here is the HTML code for the dropdown list: <label id='choose' for='options'>Sele ...

Using angularjs to include content from other files is known as

As I delve into the concept of creating directives in AngularJS, I am faced with the imminent end of Angular 1.x and the rise of Angular 2.x. The shift seems daunting, but I am determined to bridge this gap seamlessly. In my quest for clarity, I stumbled ...

Ways to address a buffered data problem in Websocket Rxjs. When trying to send a message, it is not being received by the server and instead is being stored in a

Currently, I am utilizing Websocket Rxjs within my application. The connection is successfully established with the server, and upon subscribing to it, all data is received in an array format. However, when attempting to send data back to the server, it se ...