首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我不能让反应过渡小组工作。没有进入或离开过渡

我不能让反应过渡小组工作。没有进入或离开过渡
EN

Stack Overflow用户
提问于 2021-09-27 10:46:45
回答 1查看 1.2K关注 0票数 2

嗨,我根本不能让反应过渡小组工作。有人能看一下这个,看看它为什么行不通吗?链接正确地拉入组件,但没有转换。没有过渡进来或出去,console.log不开火。我确实在控制台中收到了折旧警告,但是所有的东西都是最新的,我不确定这是否会导致它停止工作。我不知所措,所以任何帮助都将是非常感谢的。谢谢。

代码语言:javascript
复制
import * as React from 'react'
import {
    HashRouter as Router,
    Link,
    Switch,
    Route,
    Redirect,
    useParams,
    useLocation
} from "react-router-dom";
import {TransitionGroup, CSSTransition} from "react-transition-group";


const TransGroup = () =>{

    const location = useLocation();

    return(
        <>
            <div className={"w-full h-full"}>
        <Router>
            <div className={"fill w-full"}>

                <ul className={"nav overflow-hidden mb-8 flex "}>
                    <li><Link className={"bg-white p-8 m-8 text-black inline-block"} to={"/"}>Red</Link></li>
                    <li><Link className={"bg-white p-8 m-8 text-black inline-block"} to={"/purple"}>Purple</Link></li>
                    <li><Link className={"bg-white p-8 m-8 text-black inline-block"} to={"/blue"}>Blue</Link></li>
                    <li><Link className={"bg-white p-8 m-8 text-black inline-block"} to={"/yellow"}>Yellow</Link></li>
                    <li><Link className={"bg-white p-8 m-8 text-black inline-block"} to={"/pink"}>Pink</Link></li>
                    <li><Link className={"bg-white p-8 m-8 text-black inline-block"} to={"/aqua"}>Aqua</Link></li>

                </ul>
            </div>
        </Router>
        <div className={"fill content w-full h-full"}>
            <TransitionGroup>
                <CSSTransition
                    timeout={5000}
                
                    onEnter={() => {
                        console.log('FIRED!');
                    }}
                    classNames={{
                        appear: 'my-node-appear',
                        appearActive: 'my-node-active-appear',
                        appearDone: 'my-node-done-appear',
                        enter: 'my-node-enter',
                        enterActive: 'my-node-active-enter',
                        enterDone: 'my-node-done-enter',
                        exit: 'my-node-exit',
                        exitActive: 'my-node-active-exit',
                        exitDone: 'my-node-done-exit',
                    }}
                    key={location.key}

                >
            <Switch location={location}>
                <Route exact path={"/"}>
                 
                    <div className={"bg-red-500 text-white w-full h-16"}>
                        RED
                    </div>
                </Route>
                <Route exact path={"/purple"}>
                   
                    <div className={"bg-lcnuk-other text-white w-full h-16"}>
                        PURPLE
                    </div>
                </Route>
                <Route exact path={"/blue"}>
                 
                    <div className={"bg-blue-500 text-white w-full h-16"}>
                        BLUE
                    </div>
                </Route>
                <Route exact path={"/yellow"}>
                 
                    <div className={"bg-lcnuk-other text-white w-full h-16"}>
                        YELLOW
                    </div>
                </Route>
                <Route exact path={"/pink"}>
                 
                    <div className={"bg-white text-black w-full h-16"}>
                        PINK
                    </div>
                </Route>
                <Route exact path={"/aqua"}>
                
                    <div className={"bg-black text-white w-full h-16"}>
                        AQUA
                    </div>
                </Route>
                <Route path={'*'}>
                    <div>Not Found</div>
                </Route>
            </Switch>
                </CSSTransition>
            </TransitionGroup>
        </div>
            </div>
        </>
    )
}
export default TransGroup
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-19 05:28:55

在路由中使用CSSTransition时,不希望使用SwitchTransitionGroup。您希望将CSSTransition单独放置在每个路由中,然后每个路由都需要自己的ref,并且需要将引用传递给CSSTransition节点的nodeRef支柱。

下面是我在Codesandbox上做的一个工作示例:

下面的代码块说明了这一点:

代码语言:javascript
复制
///...

interface AppRoute {
  path: string;
  title: string;
  Component: React.ComponentType;
}

const appRoutes: AppRoute[] = [
  { path: "/", title: "Home", Component: HomePage },
  { path: "/page1", title: "Page 1", Component: Page1 },
  { path: "/page2", title: "Page 2", Component: Page2 }
];

///...

////// CONTENT TRANSITION ROUTER
const PageContent = withRouter(({ location }) => {
  let routeRefs: any[] = [];

  const isMatch = useCallback(
    (path: string): boolean => {
      return location.pathname === path ? true : false;
    },
    [location]
  );

  return (
    <>
      {appRoutes.map(({ path, Component }, index) => {
        routeRefs[index] = React.useRef(null);

        return (
          <Route key={index} exact path={path}>
            {() => {
              // Route callback ensures the transitions are loaded correctly
              return (
                <CSSTransition
                  nodeRef={routeRefs[index]}
                  in={isMatch(path)}
                  timeout={300}
                  classNames="fade"
                  unmountOnExit
                  appear
                >
                  <div ref={routeRefs[index]} className="fade">
                    <Component />
                  </div>
                </CSSTransition>
              );
            }}
          </Route>
        );
      })}
    </>
  );
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69345311

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档