reactjs - 使用 React Router V4 时如何防止组件立即卸载

我正在使用 react-transition-group 的标签在使用 react-router-v4 的路由之间创建动画。我使用标签根据状态的变化在我的代码中的路由之间切换。我遇到的问题是,当 a 被触发时,组件会立即卸载,然后再让退出动画有时间播放。新路线正确动画。

我试图通过关注 React Transition Group 网站上的这个页面来解决这个问题:https://reactcommunity.org/react-transition-group/with-react-router

它承认我在立即卸载组件时遇到的问题,但我的解决方案似乎不起作用。

const routes = [
  { path: '/', name: 'Dashboard', Component: Dashboard },
  { path: '/detailedview', name: 'DetailedView', Component: DetailedView },
  { path: '/dashboardloop', name: 'DashboardLoop', Component: DashboardLoop },
]

function Example() {
  return (
    <Router>
      <>
        <Container className="container">
          {routes.map(({ path, Component }) => (
            <Route key={path} exact path={path}>
              {({ match }) => (
                <CSSTransition
                  in={match != null}
                  timeout={500}
                  classNames="page"
                  unmountOnExit
                >
                  <div className="page">
                    <Component />
                  </div>
                </CSSTransition>
              )}
            </Route>
          ))}
        </Container>
      </>
    </Router>
  )
}

ReactDOM.render((
  <Example/>
), document.getElementById('root'));

如有任何帮助,我们将不胜感激!谢谢

最佳答案

聚会迟到了,您可能已经自己解决了这个问题,但是为了记录在谷歌上搜索这个问题的人:检查您的 css。

CSSTransition 依赖于四个 css 类(如文档中所述):

/* Starting style of enter animation, i.e when element is still invisible */
.page-transition-enter{
}

/* How the element will look on the page + transition (which does the animation) goes here*/
.page-transition-enter-active{
}

/* Start of exit animation, usually how the element looks on the page */
.page-transition-exit{
}

/*The end-style of exit-animation + transition */
.page-transition-exit-active{
}

React Transition Group 文档 (http://reactcommunity.org/react-transition-group/css-transition) 中的一个简单过渡示例:

.page-enter {
  opacity: 0;
}
.page-enter-active {
  opacity: 1;
  transition: opacity 200ms;
}
.page-exit {
  opacity: 1;
}
.page-exit-active {
  opacity: 0;
  transition: opacity 200ms;
}

由于您的元素在没有动画的情况下卸载但在进入时有动画,看起来您很可能遗漏了最后两个类中的某些内容。或者完全缺少一个或两个类。

https://stackoverflow.com/questions/57204090/

相关文章:

amazon-web-services - 如何使用 aws Lambda 将多个相关文件作为一个组

redux - 将 Prop 传递给 redux 初始状态

spring-boot - Spring Boot 可执行 jar 通过 Azure devops

react-native - 如何在切换到 react-navigation 之前预呈现组件?

javascript - `npm install`后出现关于python语法错误的错误?

angular - Froala 编辑器 formControl 在第二次加载后不更新

python - 如何在 Azure Functions API (Python) 中处理 CORS

reactjs - 添加具有 Material 表的组件会更改 Material-UI AppBar

python - AWS Lambda——无法为 SpaCy 导入 srsly.ujson.ujso

reactjs - react Hook : Idiomatic way to ensure tha