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

AngularJS: resolving route dependencies

I have a variable $scope.question that contains all the questions for the page. My goal is to loop through the questions page by page. To achieve this, I created a function called questionsCtrl and I am calling this function in the config while setting up ...

Adjusting the dimension of a bootstrap button based on a value using React

Just getting started in the world of React and could use some assistance achieving this specific design: https://i.stack.imgur.com/E8PAr.png The buttons/widgets displayed above represent a certain number of items. I'm looking to adjust the size of t ...

When a container is styled with 'overflow-y: auto', it will display additional horizontal and vertical scrolling bars

I have two files, one HTML and one CSS. The HTML file contains the following code: <div class="app"> <div class="header"></div> <div class="main"> <div class="container_1"> ...

Store the running of a JavaScript file in memory

It seems highly unlikely that achieving the following is possible without expert confirmation: On page number 1, user and application data is requested as soon as the page loads. Page number 2 utilizes the same script, so requesting the same information w ...

Troubleshooting a problem with dynamic options in Materialize select set

I'm currently facing an issue with my two dependent dropdowns, country and state. When I select a country, I use JavaScript to populate the respective states in the state dropdown. However, even though the options are added correctly when I inspect th ...

What is the easiest way to include a copyright symbol in a React component?

I am trying to include the copyright symbol in a React component, but it doesn't seem to be working for me. function Footer() { return ( <footer> <p>&copy</p> </footer> ); } <p>&copy</p> ...

Can data from an Angular app be accessed by an external JavaScript code within the same project?

I've been thinking about a theoretical scenario that luckily I haven't encountered yet. Imagine I have an Angular Project compiled in My PROJECT FOLDER. <br/> Inside this PROJECT FOLDER, there's another JAVASCRIPT FILE external to ...

Migrating from Styled Components to Material-UI: Is it possible for Material-UI to apply `withStyles()` to a div element

My app's logic and operation aspects are solid, so I can't risk altering components that switch from <FriendlyComponentName /> to <div css={{styleObject}} /> using Material makeStyles() or similar methods. An example of existing Styl ...

Tips for successfully executing child_process.exec within an ajax request

On a server that I have access to but not ownership of, there is a node js / express application running on port 3000. Various scripts are typically executed manually from the terminal or via cron job. My goal is to have a button on the client-side that tr ...

Error: The 'replace' property of null cannot be read in select2

In my Node Express app, I am incorporating select2, and encountering an error when supplying an array as the data source with data: dataBase. The issue arises as Uncaught TypeError: Cannot read property 'replace' of null. Although using an ajax ...

Unable to find the desired match with Regex

I'm attempting to use a regex to replace content within brackets, but I'm encountering an unexpected result. This is the text I'm trying to target: Foo (bar) Below is the regex pattern I am using: /(?=\().*(?=\))/ My expectati ...

The present URL of Next.js version 13

When working with Next.js App Router in SSR, how can I retrieve the complete URL of the current page? I am unable to use window.location.href due to the absence of a defined window object, and using useRouter() does not provide access to the full URL. ...

When attempting to utilize a dropdown to display information from a database in a React application, the value returned is undefined

When I choose an option from the dropdown menu, the value appears as undefined even though it is populated with data from the database. import { Select, MenuItem, FormControl, InputLabel, } from "@material-ui/core"; function F ...

Is it possible to optimize the memory usage when running `next build`?

My Next.js app is hosted on a 1gb memory server. However, whenever I run next build to redeploy my application, the memory usage spikes from around 70% to 100%, causing the system to slow down significantly. The build process usually takes 15-20 minutes un ...

Pass an image into an input field with the attribute type="filename" using JavaScript directly

My goal is to automate the process of uploading images by passing files directly to an input type="filename" control element using JavaScript. This way, I can avoid manually clicking [browse] and searching for a file in the BROWSE FOR FILES dialog. The re ...

Using TypeScript and the `this` keyword in SharePoint Framework with Vue

I'm currently developing a SharePoint Framework web part with Vue.js. Check out this code snippet: export default class MyWorkspaceTestWebPart extends BaseClientSideWebPart<IMyWorkspaceTestWebPartProps> { public uol_app; public render(): ...

Troubleshooting Vercel and Express DELETE request cross-origin resource sharing problem

Currently, I am in the process of developing an API using Vercel and ExpressJS. The GET and POST endpoints are functioning properly, however, I encountered an issue with the DELETE endpoint. When attempting to access the endpoint from my client-side JavaSc ...

Check out the selected values in Ionic 3

I am trying to retrieve all the checked values from a checkbox list in an Ionic3 app when clicked. Below is the code snippet: <ion-content padding> <ion-list> <ion-item *ngFor="let item of items; let i= index"> <ion-label>{{i ...

Tips for specifying Content-Encoding metadata headers using webpack compression

During development, I enjoy running npm run watch, but my uncompressed bundle size is quite large at 7.5MB. To add HTTPS support via ngrok (for team usage as well), the process of checking CSS updates with a simple page reload becomes time-consuming. I wi ...

Using Ajax to return a Post type in c# mvc 4 instead of a value

Hey there, I seem to be encountering an issue that I could use some help with. $.ajax({ type: "POST", url: "/controller/CreateList", contentType: "application/json; charset=utf-8", traditional: true, ...