The react-transition-group fails to function properly until the page is reloaded

My task scheduler using React Router 5 and React Redux is working fine.

Now I am trying to animate transitions between different routes using react-transition-group.

When I click the URL changes, but the screen does not re-render until I manually reload the page.

The animation only works when I use the browser's backward button.

I don't see any errors in the console.

What could be the issue and how can I resolve it?

Thank you

App.js

import { Fragment, useEffect, Suspense } from "react";
import { useSelector, useDispatch } from "react-redux";
import Notification from "./shared/UIElements/Notification";
import { sendCartData, fetchCartData } from "./store/cart-actions";
import Auth from "./Auth/page/Auth";
import TaskMain from "./tasks/page/TaskMain";
import TaskFilter from "./tasks/page/TaskFilter";
import MainNavigation from "./Layout/Navigation/MainNavigation";
import LoadingSpinner from "./shared/UIElements/LoadingSpinner";
import TransitionGroup from "react-transition-group/TransitionGroup";
import CSSTransition from "react-transition-group/CSSTransition";
import "../src/scss/styles.css";

import UpdateTask from "./tasks/page/UpdateTask";
import {
  BrowserRouter as Router,
  Route,
  Redirect,
  Switch,
  useLocation,
} from "react-router-dom";
import NewTask from "./tasks/page/NewTask";

let isInitial = true; // rest of the App.js code...

App.css

// CSS code for transitions...

MainNavigation.js

import React, { useState } from 'react';
import NavLinks from './NavLinks';
import SideDrawer from './SideDrawer';
import Backdrop from '../UIElements/Backdrop';

// MainNavigation component code...

NavLink.js

import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { useHistory } from "react-router-dom";
import { cartActions } from "../../store/cart-slice";

// NavLinks component code...

SideDrawer.js

import React from 'react';
import ReactDOM from 'react-dom';
import { CSSTransition } from 'react-transition-group';

// SideDrawer component code...

index.js

import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';

// index.js code...

NewTask.js

import React, { useState } from "react";
import { useHistory } from "react-router-dom";
import { useSelector, useDispatch } from "react-redux";
import { cartActions } from "../../store/cart-slice";
import { v4 as uuid } from "uuid";

// NewTask component code...

package.json

// Dependencies...

Answer №1

I believe I have found the solution with the assistance of Drew Reese.

I have organized my route components into different files, resulting in the correct rendering.

In addition, I am now using location.pathname instead of location.key.

If anyone has a better solution, please feel free to share it here.

Thank you.

App.js

import {
  Fragment,
  useEffect,
  Suspense,
} from "react";
import { useSelector, useDispatch } from "react-redux";
import Notification from "./shared/UIElements/Notification";
import { sendCartData, fetchCartData } from "./store/cart-actions";
import MainNavigation from "./Layout/Navigation/MainNavigation";
import LoadingSpinner from "./shared/UIElements/LoadingSpinner";
import TransitionGroup from "react-transition-group/TransitionGroup";
import CSSTransition from "react-transition-group/CSSTransition";
import "../src/scss/styles.css";

import {
  BrowserRouter as Router,
  useLocation,

} from "react-router-dom";
import AuthenticationApp from "./AuthenticationApp";
import TaskApp from "./TaskApp";

let isInitial = true;

function App() {
  const dispatch = useDispatch();
  const cart = useSelector((state) => state.cart);
  const notification = useSelector((state) => state.ui.notification);
  const logged = useSelector((state) => state.cart.logged);
  const location = useLocation();
  useEffect(() => {
    dispatch(fetchCartData());
    console.log(`fetch usefeect`);
  }, [dispatch]);

  useEffect(() => {
    if (isInitial) {
      isInitial = false;
      return;
    }

    if (cart.changed) {
      dispatch(sendCartData(cart));
    }
  }, [cart, dispatch]);
 
  return (
    <Router>
      <Fragment>
        {notification && (
          <Notification
            status={notification.status}
            title={notification.title}
            message={notification.message}
          />
        )}
        <MainNavigation />
        <TransitionGroup>
          <CSSTransition
            timeout={1250}
            classNames="fade"
            pathname={location.pathname}
          >
            <Suspense
              fallback={
                <div className="center">
                  <LoadingSpinner></LoadingSpinner>
                </div>
              }
            >
              {!logged && <AuthenticationApp />}
              {logged && <TaskApp />}
            </Suspense>
          </CSSTransition>
        </TransitionGroup>
      </Fragment>
    </Router>
  );
}

export default App;

AuthenticationApp.js

import { Route, Redirect, Switch, useLocation } from "react-router-dom";
import TransitionGroup from "react-transition-group/TransitionGroup";
import CSSTransition from "react-transition-group/CSSTransition";
import { Suspense } from "react";
import LoadingSpinner from "./shared/UIElements/LoadingSpinner";

import Auth from "./Auth/page/Auth";

const AuthenticationApp = ({}) => {
  const location = useLocation();

  return (
    <TransitionGroup>
      <CSSTransition timeout={1250} classNames="fade" key={location.pathname}>
        <Suspense
          fallback={
            <div className="center">
              <LoadingSpinner></LoadingSpinner>
            </div>
          }
        >
          <Switch>
            <Route path="/" exact>
              <Auth />
            </Route>
            <Redirect to="/" />
          </Switch>
        </Suspense>
      </CSSTransition>
    </TransitionGroup>
  );
};

export default AuthenticationApp;

TaskApp.js

import { Route, Redirect, Switch, useLocation } from "react-router-dom";
import TransitionGroup from "react-transition-group/TransitionGroup";
import CSSTransition from "react-transition-group/CSSTransition";
import { Suspense } from "react";
import LoadingSpinner from "./shared/UIElements/LoadingSpinner";
import TaskMain from "./tasks/page/TaskMain";
import NewTask from "./tasks/page/NewTask";
import UpdateTask from "./tasks/page/UpdateTask";
import TaskFilter from "./tasks/page/TaskFilter";

const TaskApp = () => {
  const location = useLocation();

  return (
    <TransitionGroup>
      <CSSTransition timeout={1250} classNames="fade" key={location.pathname}>
        <Suspense
          fallback={
            <div className="center">
              <LoadingSpinner></LoadingSpinner>
            </div>
          }
        >
          <Switch>
            <Route path="/" exact>
              <TaskMain />
            </Route>
            <Route path="/tasks/new" exact>
              <NewTask />
            </Route>
            <Route path="/tasks/update/:id" exact>
              <UpdateTask />
            </Route>
            <Route path="/items" exact>
              <TaskFilter />
            </Route>
            <Redirect to="/" />
          </Switch>
        </Suspense>
      </CSSTransition>
    </TransitionGroup>
  );
};

export default TaskApp;

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

What is the reason for triggering a rerender when there is a modification to a map() element using document.querySelector() in JS/next.js?

const slides = [ [string1, string2, stringi], [string1, string2, stringi], [string1, string2, stringi], [string1, string2, stringi], ]; const changeSlide = (num) => { const discipline = document.querySelector("#changeSlide-&quo ...

Material UI defines a fixed grid size at the designated breakpoint

I'm currently facing some challenges with adjusting the size of the Material UI grid. I would like to set a fixed size for the grid at specific breakpoints. For instance, at xs width, it should take up the full width, but at sm width, it should be 200 ...

hiding html elements by using the display property set to none instead of physically removing

I am currently utilizing an if-else statement to display different HTML structures. As a result, the entire HTML is being rendered twice. Is there a way we can utilize 'display: none' instead? I attempted to use it in th ...

Using dangerouslySetInnerHTML in React within a Fragment

In my current project, I have a specific requirement where I need to format text in React and also include HTML rendering. Here's an example of what I'm trying to accomplish: import React, {Fragment} from "react"; import {renderToString} from " ...

Encountering a problem while attempting to save a scss file in Visual Studio Code

As a beginner in using sass, I decided to utilize the node-sass NPM package for compiling scss files into CSS format. Upon saving the file, I encountered the following error: { "status": 3, "message": "Internal Error: File to ...

Is the default animation duration of Compass being ignored?

After recently updating to Compass version 1.0.16, I started setting up basic usage of the new animation features. However, I encountered an issue where setting default values for different animation settings did not take effect. This led me to hard-code t ...

When attempting to host a React application within an Express server, an error is encountered with the message "

I am looking to host my react app, created with create-react-app, using an express app. This is the express app I have written: const express = require('express') const path = require('path') const app = express() console.log(path.re ...

`How Does valuePropName Function?`

When submitting a form with a checkBox component without setting the valuePropName, the value of the checkBox is returning as undefined. <Form.Item name="remember" valuePropName="checked"> <Checkbox>Remember me< ...

Is it possible to utilize the OnBlur prop based on a certain condition?

To display a component when the input is focused, follow the steps below: Click here for not focused state When you click on the text input, the component should appear like this: Click here for focused state The code snippet provided works correctly. ...

What causes the button size to change in Bootstrap when switching to a spinner?

Every time I switch from a spinner back to a button, the button's size changes. I am struggling to identify the cause of this issue. function resizeButton() { var btn = document.getElementById('submitBtn'); btn.innerHTML = ' ...

Ways to eliminate unnecessary re-rendering of components that remain unchanged?

I am struggling with a component that contains various other components, such as text fields. Whenever an input is made in the text field, all the components are re-rendered. My goal is to prevent this re-rendering and only update the component that has a ...

Centering text using CSS Bootstrap

Hey everyone, I've been attempting to center my text using Bootstrap, but for some reason it's not working. Can someone help me figure out what's going on? <footer class="row p-1 mt-3 bg-primary text-white"> <p class= ...

Cross-Platform: Varied functionalities in disabled input fields (avoiding any direct replication)

My input HTML field is disabled, but I've noticed that in some browsers (such as Chrome, Edge, Internet Explorer, and Opera), it's still possible to select and copy the text. However, in Firefox, this functionality does not work. To test this yo ...

What could be causing the uneven application of CSS padding when it is applied to a div that serves as the parent of an HTML form?

Padding has been added only to the top and bottom of a div. The padding attribute is displaying consistently for the form element, however, it is not behaving uniformly for the parent div of the form as illustrated in the image provided below. Output:- h ...

the navigate feature is not redirecting to the intended page as expected

I am currently working on a React application and facing an issue with configuring the navigation. The problem lies in the code of the Navbar component, where upon clicking the logout button, the authentication context should be cleared and the user should ...

Switching between three div elements along with two icons can be done by using toggle functionality

Is there a way to make the icon change back when clicked again without making major changes to the existing code? Looking for suggestions on how to achieve this functionality. function myFunction() { var element = document.getElementById("demo"); el ...

Having trouble getting the toggle button JavaScript code to function properly in React JS?

I'm in need of some assistance with the comment section located below the JavaScript Code. I am trying to implement a toggle button / hamburger button, but it seems that addEventListener is not necessary for ReactJS. Can someone provide guidance on wh ...

Setting a default value within an input tag: A step-by-step guide

const [userData, setUserData] = useState([]); const handleUserInfo = (id) => { fetch(`https://602e7c2c4410730017c50b9d.mockapi.io/users/${id}`) .then(res => res.json()) .then(data => setUserData(data)) } <inpu ...

Developing React component libraries with TypeScript compared to Babel compiler

Currently, I'm utilizing the babel compiler for compiling my React component libraries. The initial choice was influenced by Create React App's use of the same compiler. However, I've encountered challenges with using babel for creating libr ...

react-native-iap is constantly returning an empty array when trying to fetch subscriptions

I am currently utilizing react-native-iap for in-app purchases. However, I am encountering an issue where the getSubscriptions function is returning an empty array when used on Android. Interestingly, everything works as expected on iOS. Despite my best e ...