The react transition group fails to add the necessary CSS classes to the specified div

Regardless of whether I utilize React transition group, Tailwind, pure CSS, or any other framework, no transitions seem to occur when I structure my simple component in the following manner. I have experimented with various frameworks, but the outcome remains consistent.

export const OuterComponent = () => {
  const [show, setShow] = useState(false);

  const InnerComponent = () => {
    return (
      <div>
        <CSSTransition 
          in={show}
          timeout={600}
          classNames={"sidebar-transition"}
        >
          <div className="sidebar"></div>
        </CSSTransition>
      </div>
    );
  };

  return (
    <div>
      <InnerComponent />
      <button onClick={() => setShow((prev) => !prev)}>click me</button>
    </div>
  );
};

If I structure the component as shown below, everything functions correctly:

export const OuterComponent = () => {
  const [show, setShow] = useState(false);

  const InnerComponent = ({ children }) => {
    return (
      <div>
        <div className="sidebar">{children}</div>
      </div>
    );
  };

  return (
    <div>
      <CSSTransition
        in={show}
        timeout={600}
        classNames={"sidebar-transition"}
      >
        <InnerComponent />
      </CSSTransition>
    </div>
  );
};

Here is another example using Tailwind, yielding the same result:

export const OuterComponent = () => {
  const [show, setShow] = useState(false);

  const style =
    "w-[100px] h-[60px] transition-all duration-1000 bg-purple-900 text-2xl text-white " +
    (show ? " w-[400px] " : "");

  const InnerComponent = ({ children }) => {
    return (
      <div className={style}> // Applying style solely to this div disrupts the transition
        {children}
      </div>
    );
  };

  return (
    <div className={style}> // If I apply style here and remove the above style on the InnerComponent, the transition works normally
      <InnerComponent />
    </div>
  );
};

Could someone provide insight into what might be causing this issue? Despite attempting various solutions, the problem persists, leading me to believe that a simple underlying factor eludes my understanding.

Answer №1

After some investigation, I have discovered the solution to the issue at hand. It appears that the InnerComponent is currently declared within the OuterComponent, causing it to be reinitialized every time the page is rendered and resulting in unexpected behavior. To resolve this issue, all I need to do is move the InnerComponent outside of the definition of the OuterComponent, and everything should function properly.

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

The input tag contains an empty Request.Form

My issue lies in using Request.Form to retrieve text from an input field, as the value always ends up being empty. My goal is to extract the text value from the input tag and use it to query my database. I have attempted to write to an ASP TextBox but enco ...

Keep the button in a single color unless it is being hovered over

Is there a way to create a button that changes color on hover, then reverts back to its original color after it's clicked? I'm facing an issue where my button initially has a red gradient, then changes color when hovered over, but turns white on ...

There is a JSON issue present, with half of the variables being undefined

Having an issue with my JSON as it throws me an undefined error. What's puzzling is that it recognizes my first variable without any problem. Below are the codes for better understanding: JSON {"Comics":[ {"comic" : { "src":"Pictures/Comic ...

Having trouble with the visibility of the hover background?

I have a goal in mind: This is the outcome I've achieved so far: LINK My aim is to have a white background with a blue border appear at the bottom when hovering over navigation links. Unfortunately, this isn't happening as expected. Can anyone ...

Looking to enhance the accessibility of a dynamically generated file by implementing the ability to expand and collapse sections as parent nodes

Currently working on a webpage where I am showcasing a testsuite file as the parent node and test cases within the testsuite file as child nodes. I have added checkboxes to both the parent and child nodes. Now, my goal is to include an ex ...

Enhance the aesthetics of material-ui tooltips by utilizing @emotion/styled

Can material-ui tooltips be customized using the styled function from @emotion/styled? import { Tooltip } from '@material-ui/core'; import styled from '@emotion/styled'; const MyTooltip = styled(Tooltip)` // customize the tooltip ...

The behavior exhibited by node.js express is quite peculiar

I am currently running an Express server. My process involves uploading an Excel File from HTML, which is then parsed by Express to perform calculations. Each row in the Excel file contains information about a User's address. For each address, our ...

Animation using CSS keyframes for simultaneous manipulation of various properties

I am facing a challenge with animating both the bottom and left properties of an object inside a container div simultaneously. How can I make it move diagonally? Despite using keyframes, the following code does not seem to achieve this effect: #containe ...

Create a choppy distortion effect using CSS filters in Google Chrome

Currently working on a hover effect that enhances image brightness and scales the image during hover. However, I am experiencing some choppiness in the transform animation due to the CSS filter. It's strange because it appears to be smooth in Safari a ...

Ensure that the HTML image and text are positioned side by side, with the text appearing alongside the picture rather than below

Looking for a way to keep the text aligned to the right of an image, regardless of length? Check out this exercise: https://www.example.com/css/exercise-1 Is there a method to maintain the text layout next to an image while accommodating varying amounts o ...

Ways to troubleshoot JavaScript following an AJAX request?

My webpage is structured into three separate files. The index.html file contains a navigation bar, a content box, and a footer within 3 divs. Additionally, there are two other .html files with different content that should load upon clicking specific links ...

What is the best way to extend an inline-block element to cover the entire text line?

Let's talk about the scenario: https://i.stack.imgur.com/we05U.png In the image, there is a container (displayed as a div) with only one child - a span element. The goal is to introduce a second child that occupies the entire red space depicted in t ...

Design a fluid row that allows for horizontal scrolling with text alignment options on the left, center, and right side

I created a row with horizontal scrolling on smaller screens. Is there a way to have text aligned left, center, and right all on the same line? I want to achieve this with text aligned in multiple positions. Check out the demonstration on Stackblitz C ...

When the next button is clicked, the button content disappears

I am struggling with a problem involving two buttons that store tables. The issue is, I want the table to disappear when the second button is clicked and show the contents of the second button immediately after clicking it once, rather than having to click ...

The functionality of activating a button is not functioning as expected in AngularJS framework

Next to the question I have linked here, I am exploring a different approach. As a beginner in AngularJS/Cordova/Ionic, my goal is to achieve different outcomes when the "Eingepasst" button is clicked, each with unique logic compared to "Gewonnen" or "Ver ...

The nodemon tool seems to be malfunctioning, displaying an error message stating that the command `'.' is not recognized as a valid internal or external command, operable program, or batch file.`

view screenshot Command executed in terminal: D:\Mern projects\movie_app> npm run dev [email protected] dev ./node_modules/.bin/nodemon src/index.js Error message received: '.' is not recognized as an internal or external co ...

Generate a barcode using `npm react-barcodes print barcode`

Trying to print a barcode from ReactJS to a Zebra printer has been a bit challenging. After using the npm package react-barcodes to generate the barcode, I followed the instructions in this post but unfortunately, the barcodes are not turning out correctl ...

Angular2: Dynamically switch between selected checkboxes in a list

Is there a way to toggle a checked checkbox list in Angular2? I am trying to create a button that, when pressed while the full list is visible, will display only the checked items. Pressing the button again would show the entire list. Here's the Plu ...

I'm looking to enhance my code by adding a new user login password. How can I achieve this?

Currently, I am in the process of adding another user and have been exploring various code snippets available on different websites. I decided to test the following example, which worked perfectly. However, I am now wondering how I can add an additional u ...

Preventing preventDefault() from firing in JQuery only when necessary

I have come up with a script that I recently created. <script> $(document).ready(function(){ $('a').data('loop',true); $('body').on('click', 'a', function(event){ console.lo ...