Reputation: 543
I am trying to use CSSTransition component from react-transition-group module to implement page transitions as a user switches from one route to another. From my code tested below, it generates the following error:
Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports
Here is my code sample:
import React from 'react';
import { BrowserRouter as Router, Route, Switch, useParams, useLocation } from 'react-router-dom';
import Home from './Home';
import Login from './Login';
import SignUp from './SignUp';
import ManagerSignUp from './ManagerSignUp';
import ManagerLogin from './ManagerLogin';
import CreateEvent from './CreateEvent';
import RegisterVenue from './RegisterVenue';
import Dashboard from './Dashboard';
import ManagerDashboard from './ManagerDashboard';
import EventPage from './EventPage';
import './assets/pageTransitions.css'
import {
TransitionGroup,
CSSTransition
} from "react-transition-group";
const routes = [
{path: '/', name: 'Home', Component: Home },
{path: '/login', name: 'Login', Component: Login },
{path: '/signup', name: 'Signup', Component: SignUp },
{path: '/manager_signup', name: 'Manager Signup', Component: ManagerSignUp },
{path: '/manager_login', name: 'Manager Login', Component: ManagerLogin },
{path: '/eventcreate', name: 'Event Create', Component: CreateEvent },
{path: '/registervenue', name: 'Register Venue', Component: RegisterVenue },
{path: '/dashboard', name: 'Dashboard', Component: Dashboard },
{path: '/manager_dashboard', name: 'Manager Dashboard', Component: ManagerDashboard },
{path: '/event/:uid', name: 'Event Page', Component: EventPage },
]
export default function App() {
// let {location} = window.location.origin
return (
<React.Fragment>
<Router>
<Switch>
<Route>
{routes.map(({ path, Component }) => (
<Route key={path} exact path={path}>
{({match}) => (
<CSSTransition
in={match != null}
timeout={300}
classNames="page"
unmountOnExit
>
<Component />
</CSSTransition>
)}
</Route>
))}
</Route>
</Switch>
</Router>
</React.Fragment>
);
}
I can't determine the nature of this error after multiple crosschecks. I need help determining why this occurs & how to fix it please.
Upvotes: 2
Views: 758
Reputation: 2595
I found some errors here replace your Switch code with this one
you are not passing component here and you are putting your all Route in tag which is wrong
<Switch>
{routes.map(({ path, Component }) => (
<Route key={path} exact path={path} component={Component} >
</Route>
))}
</Switch>
For CSSTransition you can create one HOC(Higher Order Component ) and use it with your all components like component={HOC(Component)}
HOC :
import React , {useState,useEffect} from 'react';
import {
TransitionGroup,
CSSTransition
} from "react-transition-group";
export default (ComposedComponent) => {
return (props) =>{
/* HOC : component is like write once and use any where.
You can pass your any component it will return with your CSSTransition
for that component */
retun (
<ComposedComponent {...props}>
{({match}) => (
<CSSTransition
in={match != null}
timeout={300}
classNames="page"
unmountOnExit
>
{props.children}
</CSSTransition>
)}
</ComposedComponent>
)
};
};
Final step is import your HOC to your file and pass your component to it like
import HOC from 'path of HOC'
<Route key={path} exact path={path} component={HOC(Component)} >
Upvotes: 1