简体   繁体   English

尝试将 react-transition-group 实现到 react-router 中会产生错误:“元素类型无效......”

[英]Trying to implement react-transition-group into react-router generates error : 'Element type is invalid ...'

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.我正在尝试使用 react-transition-group 模块中的 CSSTransition 组件来实现页面转换,因为用户从一个路由切换到另一个路由。 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.我需要帮助确定为什么会发生这种情况以及如何解决它。

I found some errors here replace your Switch code with this one我在这里发现了一些错误,用这个替换你的 Switch 代码

you are not passing component here and you are putting your all Route in tag which is wrong您没有在此处传递组件,而是将所有 Route 放在错误的标记中

    <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)}对于 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>
    )
  };
};

Final step is import your HOC to your file and pass your component to it like最后一步是将您的 HOC 导入您的文件并将您的组件传递给它,例如

   import HOC from 'path of HOC'

  <Route key={path} exact path={path} component={HOC(Component)} >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM