Trying to implement react-transition-group into react-router generates error : 'Element type is invalid ...'
Trying to implement react-transition-group into react-router generates error : 'Element type is invalid ...'
我正在尝试使用 react-transition-group 模块中的 CSSTransition 组件来实现页面转换,因为用户从一个路由切换到另一个路由。
根据我在下面测试的代码,它会生成以下错误:
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
这是我的代码示例:
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>
);
}
经过多次交叉检查,我无法确定此错误的性质。我需要帮助确定为什么会发生这种情况以及如何解决它。
我发现这里有一些错误 用这个替换你的 Switch 代码
你没有在此处传递组件,而是将所有路由放在错误的标签中
<Switch>
{routes.map(({ path, Component }) => (
<Route key={path} exact path={path} component={Component} >
</Route>
))}
</Switch>
对于 CSSTransition,您可以创建一个 HOC(高阶组件)并将其与您的所有组件一起使用,例如 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>
)
};
};
最后一步是将您的 HOC 导入您的文件并将您的组件传递给它,就像
import HOC from 'path of HOC'
<Route key={path} exact path={path} component={HOC(Component)} >
我正在尝试使用 react-transition-group 模块中的 CSSTransition 组件来实现页面转换,因为用户从一个路由切换到另一个路由。 根据我在下面测试的代码,它会生成以下错误:
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
这是我的代码示例:
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>
);
}
经过多次交叉检查,我无法确定此错误的性质。我需要帮助确定为什么会发生这种情况以及如何解决它。
我发现这里有一些错误 用这个替换你的 Switch 代码
你没有在此处传递组件,而是将所有路由放在错误的标签中
<Switch>
{routes.map(({ path, Component }) => (
<Route key={path} exact path={path} component={Component} >
</Route>
))}
</Switch>
对于 CSSTransition,您可以创建一个 HOC(高阶组件)并将其与您的所有组件一起使用,例如 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>
)
};
};
最后一步是将您的 HOC 导入您的文件并将您的组件传递给它,就像
import HOC from 'path of HOC'
<Route key={path} exact path={path} component={HOC(Component)} >