愚墨的博客
  • 首页
  • 前端技术
  • 面试
只争朝夕不负韶华
  1. 首页
  2. 前端框架
  3. React
  4. 正文

react-transition-group 升级指南

2017年07月26日 2996点热度 0人点赞 0条评论

这只是一篇个人记录文章。

 

使用react-router切换页面显得有些单调乏味。所以我们会使用react-transition-group 来进行一些动画的设置,主要是路由的进出动画,当然也可以做其他的动画展示,只是css3 的问题。

react-transition-group 用的大多是1.2.0 或者 1.1.1 的版本,现在是2.2.2的版本,API基本上都变化了。

Note:ReactTransitionGroup和ReactCSSTransitionGroup已被移动到由社区维护的react-transition-group包。 它的1.x分支与现有的插件完全是API兼容的。 请在新的存储库中提出错误和功能请求。

 

  • transitionName -> classNames
  • transitionEnterTimeout and transitionLeaveTimeout -> timeout={{ exit, enter }}
  • transitionAppear -> appear
  • transitionEnter -> enter
  • transitionLeave -> exit

 

API 的更换对应着css的class也需要更换 ,不过只需更换一个。leave->exit

官方文档中已经给了 两个Demo 对比,这里我就把自己的Demo写一下。

相同的功能,分别用v1 和v2 来写一下。

功能就是路由的切换动画,

这里依赖的版本是

"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-router": "^4.1.2",
"react-router-dom": "^4.1.2",

 

V1 版本

"react-transition-group": "^1.1.1"

import React from 'react';
import {render} from 'react-dom';
import {AppContainer} from 'react-hot-loader';
import {Link, BrowserRouter, Route, NavLink, Switch} from 'react-router-dom';
import {CSSTransitionGroup} from 'react-transition-group';
import Com1 from './components/com1';
import Com2 from './components/com2';
import Com3 from './components/com3';
import './app.css'
const leftNav = () => {
  return (
    <nav>
      <ul>
        <li><NavLink activeStyle={{color: 'blue'}} to="/com1">com1</NavLink></li>
        <li><NavLink activeStyle={{color: 'blue'}} to="/com2">com2</NavLink></li>
        <li><NavLink activeStyle={{color: 'blue'}} to="/com3">com3</NavLink></li>
      </ul>
    </nav>
  )
};
class App extends React.Component {
  render() {
    return (
      <BrowserRouter>
        <Route render={({location}) => (
          <div style={{position: 'absolute', left: '400px'}}>
            <div>
              {leftNav()}
            </div>
            <div>
              <CSSTransitionGroup
                transitionName="example"
                transitionAppear={true}
                transitionAppearTimeout={500}
                transitionEnterTimeout={500}
                transitionLeaveTimeout={300}
              >
                <div key={location.pathname}
                     style={{position: 'absolute', width: '200px', background: 'lightblue', height: '100px'}}>
                  <Switch key={location.key} location={location}>
                    <Route exact path="/" component={Com1}/>
                    <Route exact path="/com1" component={Com1}/>
                    <Route exact path="/com2" component={Com2}/>
                    <Route exact path="/com3" component={Com3}/>
                  </Switch>
                </div>
              </CSSTransitionGroup>
            </div>
          </div>
        )}/>
      </BrowserRouter>
    )
  }
};

var root = document.getElementById('app');
render(
  <App/>
  , root)

对应的CSS

.example-enter {
    opacity: 0;
    transform: translateX(100%);
}

.example-enter.example-enter-active {
    opacity: 1;
    transform: translateX(0);
    transition: all .5s ease-in;
}

.example-leave {
    opacity: 1;
    transform: translateX(0);
}

.example-leave.example-leave-active {
    opacity: 0;
    transform: translateX(-100%);
    transition: all .3s ease-out;
}


.example-appear {
    opacity: 0.01;
}
.example-appear.example-appear-active {
    opacity: 1;
    transition: opacity .5s ease-in;
}

V2 版本

"react-transition-group": "^2.2.0"

import React from 'react';
import {render} from 'react-dom';
import {AppContainer} from 'react-hot-loader';
import {Link, BrowserRouter, Route, NavLink, Switch} from 'react-router-dom';
import {TransitionGroup,CSSTransition} from 'react-transition-group';
import Com1 from './components/com1';
import Com2 from './components/com2';
import Com3 from './components/com3';
import './app.css'
const leftNav = () => {
  return (
    <nav>
      <ul>
        <li><NavLink activeStyle={{color: 'blue'}} to="/com1">com1</NavLink></li>
        <li><NavLink activeStyle={{color: 'blue'}} to="/com2">com2</NavLink></li>
        <li><NavLink activeStyle={{color: 'blue'}} to="/com3">com3</NavLink></li>
      </ul>
    </nav>
  )
};
class App extends React.Component {
  render() {
    return (
      <BrowserRouter>
        <Route render={({location}) => (
          <div style={{position: 'absolute', left: '400px'}}>
            <div>
              {leftNav()}
            </div>
            <div>
              <TransitionGroup>
                <CSSTransition key={location.key} classNames="example" timeout={{enter: 500, exit: 300}}>
                <div key={location.pathname}
                     style={{position: 'absolute', width: '200px', background: 'lightblue', height: '100px'}}>
                  <Switch key={location.key} location={location}>
                    <Route exact path="/" component={Com1}/>
                    <Route exact path="/com1" component={Com1}/>
                    <Route exact path="/com2" component={Com2}/>
                    <Route exact path="/com3" component={Com3}/>
                  </Switch>
                </div>
                </CSSTransition>
              </TransitionGroup>
            </div>
          </div>
        )}/>
      </BrowserRouter>
    )
  }
};

var root = document.getElementById('app');
render(
  <App/>

对应的CSS

.example-enter {
    opacity: 0;
    transform: translateX(100%);
}

.example-enter.example-enter-active {
    opacity: 1;
    transform: translateX(0);
    transition: all .5s ease-in;
}

.example-exit {
    opacity: 1;
    transform: translateX(0);
}

.example-exit.example-exit-active {
    opacity: 0;
    transform: translateX(-100%);
    transition: all .3s ease-out;
}


.example-appear {
    opacity: 0.01;
}
.example-appear.example-appear-active {
    opacity: 1;
    transition: opacity .5s ease-in;
}

具体对应关系在官方文档中已经很明确的指出,这里不再赘述。

 

标签: 暂无
最后更新:2017年07月26日

愚墨

保持饥渴的专注,追求最佳的品质

点赞
< 上一篇
下一篇 >

文章评论

取消回复

搜搜看看
历史遗迹
  • 2023年5月
  • 2022年9月
  • 2022年3月
  • 2022年2月
  • 2021年12月
  • 2021年8月
  • 2021年7月
  • 2021年5月
  • 2021年4月
  • 2021年2月
  • 2021年1月
  • 2020年12月
  • 2020年11月
  • 2020年9月
  • 2020年7月
  • 2020年5月
  • 2020年4月
  • 2020年3月
  • 2020年1月
  • 2019年5月
  • 2019年3月
  • 2019年2月
  • 2019年1月
  • 2018年9月
  • 2018年3月
  • 2018年2月
  • 2018年1月
  • 2017年11月
  • 2017年7月
  • 2017年6月
  • 2017年3月
  • 2017年2月
  • 2017年1月
  • 2016年12月
  • 2016年11月
  • 2016年9月
  • 2016年8月
  • 2016年7月
  • 2016年6月
  • 2016年5月
  • 2016年4月
  • 2016年3月
  • 2016年2月
  • 2016年1月
  • 2015年12月
  • 2015年10月
  • 2015年9月
  • 2015年7月
  • 2015年6月
  • 2015年4月

COPYRIGHT © 2020 愚墨的博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS