[英]How to fix React Router v4 page transition animation bug?
I want to create a simple fade animation when switching between routes in React Router v4.我想在 React Router v4 中的路由之间切换时创建一个简单的淡入淡出动画。
My approach is entirely based on this guy's tutorial , where he used react-transition-group
's TransitionGroup
and CSSTransition
to create the effect.我的方法完全基于这个人的教程,他使用
react-transition-group
的TransitionGroup
和CSSTransition
来创建效果。
So far the fading effect works, but not fully.到目前为止,褪色效果有效,但并不完全。
Whenever I navigate to a new route, the same component appears twice.每当我导航到新路线时,相同的组件都会出现两次。 One above the other, with the bottom one fading away quickly.
一个在另一个之上,底部的一个迅速消失。
How can I get around this?我怎样才能解决这个问题?
So far, the only way I could make the double appearing bug disappear, is by setting each child component to position:fixed
.到目前为止,我可以让双重出现的错误消失的唯一方法是将每个子组件设置为
position:fixed
。 However, since the length of each component is dynamic, the footer
's position gets screwed up.然而,由于每个组件的长度是动态的,
footer
的位置会被搞砸。 Sometimes causing the footer to overlap with the component above.有时会导致页脚与上面的组件重叠。
App.js应用程序.js
import React, { Component } from "react";
import { Switch, Route } from "react-router-dom";
import { CSSTransition, TransitionGroup } from "react-transition-group";
// Sass styling
import "./sass/main.scss";
// Website components
import Navbar from "./components/navbar/Navbar";
import Homepage from "./components/mainContent/homepage/Homepage";
import Menu from "./components/mainContent/menu/Menu";
import HowTo from "./components/mainContent/howto/HowTo";
import ContactUs from "./components/mainContent/contactus/ContactUs";
import Footer from "./components/footer/Footer";
import Error from "./components/Error";
// Website parent component
class App extends Component {
render() {
return (
<>
<Navbar />
<Route
render={({ location }) => (
<TransitionGroup>
{/* React router transitions */}
<CSSTransition key={location.key} timeout={300} classNames="fade">
<Switch>
{/* Website routes with specified url endings */}
<Route path="/" component={Homepage} exact />
<Route path="/menu" component={Menu} exact />
<Route path="/how-to" component={HowTo} exact />
<Route path="/contact-us" component={ContactUs} exact />
{/* Route 404 not found */}
<Route component={Error} />
</Switch>
</CSSTransition>
</TransitionGroup>
)}
/>
<Footer />
</>
);
}
}
export default App;
style.css样式文件
// React router transitions
.fade-appear,
.fade-enter {
opacity: 0;
z-index: 1;
}
.fade-appear-active,
.fade-enter.fade-enter-active {
opacity: 1;
transition: opacity 300ms linear;
}
.fade-exit {
opacity: 1;
}
.fade-exit.fade-exit-active {
opacity: 0;
transition: opacity 300ms linear;
}
I am open to other approaches to make page transitions in ReactJS, so any help is really appreciated!我对在 ReactJS 中进行页面转换的其他方法持开放态度,因此非常感谢任何帮助! Thanks.
谢谢。
I know its old but this is the solution:我知道它很旧,但这是解决方案:
You need to use你需要使用
Switch location={location}
Hope it helps.希望能帮助到你。
You can use the react-transition-group lib.您可以使用 react-transition-group 库。 Find doc and example code here .
在此处查找文档和示例代码。 Note that using Switch is prohibited.
请注意,禁止使用 Switch。
Regarding CSS and the footer's position being screwed up, the example also use a container position: relative
and a page position: absolute
.关于 CSS 和页脚的位置被搞砸了,该示例还使用了容器
position: relative
和页面position: absolute
。 But I have successfully implemented react-transition-group using CSS grid.但是我已经使用 CSS 网格成功实现了 react-transition-group。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.