使用react-router和react-addons-css-transition-group进行页面转换

时间:2016-06-23 13:22:28

标签: javascript reactjs transition react-router

我最近开始学习React.js,现在我正在尝试使用动画/过渡。我正在使用 React Router 并尝试在 ReactCSSTransitionGroup 的页面之间实现非常简单的转换。它在我加载/刷新页面时有效,但在通过导航链接从页面到页面时却无效。尝试使用下一个代码使页面淡入,但它不起作用:

的package.json

{
  "name": "reactapp",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "dependencies": {
    "babel-core": "^6.9.1",
    "babel-preset-react": "^6.5.0",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.9.0",
    "react": "^15.1.0",
    "react-dom": "^15.1.0",
    "webpack": "^1.13.1",
    "webpack-dev-server": "^1.14.1"
  },
  "devDependencies": {},
  "scripts": {
    "start": "webpack-dev-server --inline --content-base . --history-api-fallback"
  },
  "author": "",
  "license": "ISC"
}

的index.html

<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8">
      <title>React App</title>
      <link rel="stylesheet" href="/app.css">
   </head>
   <body>
      <div id="app"></div>
      <script src="/index.js"></script>
   </body>
</html>

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, Link, browserHistory, IndexRoute  } from 'react-router'
import ReactCSSTransitionGroup from 'react-addons-css-transition-group';

class App extends React.Component {
   render() {
      return (
         <div>
            <ul>
               <Link to="/home">Home</Link>
               <Link to="/about">About</Link>
               <Link to="/contact">Contact</Link>
            </ul>
            <ReactCSSTransitionGroup transitionName="example" transitionAppear={true} transitionAppearTimeout={500}>
                     {this.props.children}
            </ReactCSSTransitionGroup>

         </div>
      )
   }
}

export default App;

class Home extends React.Component {
   render() {
      return (
         <div>
            <h1>Home...</h1>
         </div>
      )
   }
}

export default Home;

class About extends React.Component {
   render() {
      return (
         <div>
            <h1>About...</h1>
         </div>
      )
   }
}

export default About;

class Contact extends React.Component {
   render() {
      return (
         <div>
            <h1>Contact...</h1>
         </div>
      )
   }
}

export default Contact;

ReactDOM.render((
   <Router history={browserHistory}>
      <Route path = "/" component = {App}>
         <IndexRoute component = {Home} />
         <Route path = "home" component = {Home} />
         <Route path = "about" component = {About} />
         <Route path = "contact" component = {Contact} />
      </Route>
   </Router>

), document.getElementById('app'))

app.css

.example-appear {
  opacity: 0.01;
}

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

任何想法如何使这项工作?

谢谢!

1 个答案:

答案 0 :(得分:5)

https://github.com/reactjs/react-router/blob/master/examples/animations/app.js

不是只渲染this.props.children,而是必须克隆组件,将子项作为道具传递给它。然后React-router使用该键进行导航。

<ReactCSSTransitionGroup
    transitionName="example"
    transitionAppear={true}
    transitionAppearTimeout={500}
>
    {React.cloneElement(this.props.children, { key: this.props.location.pathname })}
</ReactCSSTransitionGroup>