想用react-transition-group实现一个路由切换过度效果,但是无效

发布于 2022-09-05 21:16:09 字数 1620 浏览 12 评论 0

参照react-transition-group官网v2的说明和react-routerv4的例子,关键代码如下

 <Route render={({location}) => (
              <div>
                <Route exact path="/" render={() => (<Redirect to="/roomMain"/>)}/>
                <TransitionGroup>
                  {routes.map((route, index) => {
                    if (route.protected === 'yes') {
                      return (
                        <CSSTransition key={index} timeout={{enter: 500, exit: 300}} classNames="fade">
                          <PrivateRoute location={location}
                                        key={index}
                                        path={route.path}
                                        component={route.component}
                                        exact={true}/>
                        </CSSTransition>
                      )
                    } else {
                      return (
                        <CSSTransition key={index} timeout={{enter: 500, exit: 300}} classNames="fade">
                          <Route location={location}
                                 key={index}
                                 path={route.path}
                                 component={route.component}
                                 exact={true}/>
                        </CSSTransition>
                      )
                    }
                  })}
                </TransitionGroup>
              </div>
            )}/>

实际并没有效果,我把react-transition-group切换到v1用CSSTransitionGroup也是没有效果的。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚气少女 2022-09-12 21:16:09

解决了,贴下修改后的代码


        <Route render={({location}) => (
          <div>
            <Route exact path="/" render={() => (<Redirect to="/roomMain"/>)}/>
            <TransitionGroup>
              <CSSTransition key={location.key}
                             timeout={{enter: 500, exit: 300}}
                             classNames="fade">
                <Switch location={location}>
                  {routes.map((route, index) => {
                    if (route.protected === 'yes') {
                      return (
                        <PrivateRoute location={location}
                                      key={index}
                                      path={route.path}
                                      component={route.component}
                                      exact/>
                      )
                    } else {
                      return (
                        <Route location={location}
                               key={index}
                               path={route.path}
                               component={route.component}
                               exact/>
                      )
                    }
                  })}
                </Switch>
              </CSSTransition>
            </TransitionGroup>
          </div>
        )}/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文