写这个只是更好的梳理下我实现过程当中遇到的奇奇怪怪的问题,react
由于着实浪费了我很多时间…确定有很多也碰到过其中的问题bash
但愿对小伙伴有所帮助。react-router
spread
的效果,其实就是结合放大和旋转以及透明度的特性fade
styled-components@3.4.2
: 写样式的react-transition-group@2.4.0
: 路由过渡的,react
官方的react-router-dom@4.3.1
: react
自家路由react@16.4.2
就是再次进入路由切换的时候,以前的元素尚未消失,而新的组件渲染了,同时出现app
position:absolute
来负责渲染区域便可position:relative
, 否则会一直往上找相对位置,实在找不到会相对窗口shouldComponentUpdate
来判断URL而后阻止渲染,发现不可行location.key
是随机性的,因此组件每次都会从新渲染Link
组件,直接用事件绑定(history.push
来跳转),完美CSSTransition
的特性,由于location.key
是随机性的,不一样值都会走一遍;Math.random()
默认返回0~1随机浮点数,这里只有两个就不用成个数了// 路由跳转
gotoUrl = itemurl => {
// 拿到路由相关的信息
const { history, location } = this.props;
// 判断咱们传入的静态路由表的路径是否和路由信息匹配
// 不匹配则容许跳转,反之打断函数
if (location.pathname === itemurl) {
return;
} else {
history.push(itemurl);
}
};
复制代码
import React, { Component } from 'react';
import { TransitionGroup, CSSTransition } from 'react-transition-group';
import { Route, Redirect, withRouter, Switch } from 'react-router-dom';
import styled from 'styled-components';
import { observer, inject } from 'mobx-react';
import asyncComponent from 'components/asyncComponent/asyncComponent';
const RouterAnimationClass = styled.div`
.fade-appear,
.fade-enter {
opacity: 0;
}
.fade-appear-active,
.fade-enter-active {
transition: opacity 0.3s linear;
opacity: 1;
}
.fade-exit {
transition: opacity 0.2s linear;
opacity: 1;
}
.fade-exit-active {
opacity: 0;
}
.spread-appear,
.spread-enter {
opacity: 0.5;
transform: scale(0) rotate(30deg);
}
.spread-appear-active,
.spread-enter-active {
opacity: 1;
transform: scale(1) rotate(0);
transition: transform 0.3s ease-in-out;
}
.spread-exit {
transition: transform 0.2s ease-in-out;
transform: scale(1.2) rotate(-30deg);
}
.spread-exit-active {
transform: scale(0) rotate(0);
}
.page-content {
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
width: 100%;
}
`;
const Monitor = asyncComponent(() => import('pages/DashBoard/Monitor'));
const Analyze = asyncComponent(() => import('pages/DashBoard/Analyze'));
import ErrorPage from 'pages/Error/Error'; // 报错页面
@inject('auth')
@withRouter
@observer
class Container extends Component {
constructor(props) {
super(props);
}
render() {
const { location } = this.props;
return (
<RouterAnimationClass>
<TransitionGroup>
<CSSTransition
key={location.key}
classNames={
['fade', 'spread'][parseInt(Math.random(), 10)]
}
timeout={1000}>
<div className="page-content">
<Switch location={location}>
<Route
path="/dashboard/monitor"
exact
component={Monitor}
/>
<Route
path="/dashboard/analyze"
exact
component={Analyze}
/>
<Redirect
exact
from="/"
to="/dashboard/monitor"
/>
<Route component={ErrorPage} />
</Switch>
</div>
</CSSTransition>
</TransitionGroup>
</RouterAnimationClass>
);
}
}
export default Container;
复制代码