使用react-router實(shí)現(xiàn)單頁面應(yīng)用時(shí)設(shè)置頁面間過渡的兩種方式

1. 官方方法(推薦)

https://github.com/reactjs/react-router/tree/master/examples/animations

//--js
import React from 'react';
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';

class App extends React.Component {
  render() {
    return (
      <div className="app">
        <ReactCSSTransitionGroup
          component="div"
          className="transition-wrapper"
          transitionName="example"
          transitionEnterTimeout={500}
          transitionLeaveTimeout={500}>
          //注意這一部分很關(guān)鍵酿联,不能直接使用{this.props.children}
          {React.cloneElement(this.props.children, {
            key: this.props.location.pathname
          })}
        </ReactCSSTransitionGroup>
      </div>
    );
  }
}

module.exports = App;

//--css
.example-enter {
  opacity: 0.01;
  transition: opacity .5s ease-in;
}

.example-enter.example-enter-active {
  opacity: 1;
}

.example-leave {
  opacity: 1;
  transition: opacity .5s ease-in;
}

.example-leave.example-leave-active {
  opacity: 0;
}

.transition-wrapper {
  height: 100%;
}

2. react-router-transition

https://github.com/maisano/react-router-transition

1. 安裝

npm install --save react-router-transition

2. 使用

//--js
import React from 'react';
import RouteTransition from 'react-router-transition';

class App extends React.Component {
  render() {
    return (
      <div className="app">
        <RouteTransition
          pathname={this.props.location.pathname}
          atEnter={{ opacity: 0 }}
          atLeave={{ opacity: 0 }}
          atActive={{ opacity: 1 }}
          className="transition-wrapper">
          {this.props.children}
        </RouteTransition>
      </div>
    );
  }
}

module.exports = App;

//--css
html,
body,
#root,
.app,
.transition-wrapper,
.transition-wrapper>div {
  height: 100%;
}

//--生成的html結(jié)構(gòu)
<div class='transition-wrapper'>
  <div style='opacity: 1'>
    <div class='app'>
      ...
    </div>
  </div>
</div>

3. 模塊打包問題

目前npm上的react-router-transition模塊有問題娘侍,package.json配置的入口文件為lib/react-router-transition.js,該文件為webpack編譯打包后的文件,不能再次打包导梆,所以實(shí)際使用時(shí)需要導(dǎo)入src/RouteTransition.js文件斤讥,結(jié)合webpack.config.js配置如下:

var node_modules_dir = path.join(__dirname, 'node_modules');

resolve: {
  extensions: ['', '.js', 'jsx'],
  alias: {
    'react-router-transition': path.resolve(node_modules_dir,'react-router-transition/src/RouteTransition.jsx')
  }
}

參考

Animated transitions
maisano/react-router-transition
misterfresh/react-easy-transition
reactjs/react-router
react + react-router transition
What is the correct way to animate/transition between routes in react router

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末提澎,一起剝皮案震驚了整個(gè)濱河市尾抑,隨后出現(xiàn)的幾起案子镰踏,更是在濱河造成了極大的恐慌函筋,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件奠伪,死亡現(xiàn)場離奇詭異跌帐,居然都是意外死亡首懈,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進(jìn)店門谨敛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來究履,“玉大人,你說我怎么就攤上這事佣盒】嫱啵” “怎么了?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵肥惭,是天一觀的道長盯仪。 經(jīng)常有香客問我,道長蜜葱,這世上最難降的妖魔是什么全景? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮牵囤,結(jié)果婚禮上爸黄,老公的妹妹穿的比我還像新娘。我一直安慰自己揭鳞,他們只是感情好炕贵,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著野崇,像睡著了一般称开。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上乓梨,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天鳖轰,我揣著相機(jī)與錄音,去河邊找鬼扶镀。 笑死蕴侣,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的臭觉。 我是一名探鬼主播昆雀,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼蝠筑!你這毒婦竟也來了忆肾?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤菱肖,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后旭从,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體稳强,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡场仲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了退疫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片渠缕。...
    茶點(diǎn)故事閱讀 40,742評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖褒繁,靈堂內(nèi)的尸體忽然破棺而出亦鳞,到底是詐尸還是另有隱情,我是刑警寧澤棒坏,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布燕差,位于F島的核電站,受9級特大地震影響坝冕,放射性物質(zhì)發(fā)生泄漏徒探。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一喂窟、第九天 我趴在偏房一處隱蔽的房頂上張望测暗。 院中可真熱鬧,春花似錦磨澡、人聲如沸碗啄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽稚字。三九已至,卻和暖如春秩命,著一層夾襖步出監(jiān)牢的瞬間尉共,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工弃锐, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留袄友,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓霹菊,卻偏偏與公主長得像剧蚣,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子旋廷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內(nèi)容