ReactCSSTransitionGroup 与 reactJS 15 和 ReactRouter 4

ReactCSSTransitionGroup with reactJS 15 and ReactRouter 4

我想为 React Router 创建一个过渡动画,但它似乎不起作用(没有过渡)。

正如你在下面看到的,我所做的就是创建一个组件 FadeRoute,它使用 ReactCSSTransitionGroup 来创建动画,然后我在 react-router-dom

的开关组件中使用它

这是我的反应代码

import { BrowserRouter as Router,  Switch, Route } from "react-router-dom";
import {  browserHistory } from "react-router";
import ReactCSSTransitionGroup from 'react-addons-css-transition-group'


function FadeRoute({ component: Component, ...rest }) {
    return (
        <Route {...rest} children={({ location, match }) => (
            <ReactCSSTransitionGroup   
                    transitionName="fade"
                    transitionEnterTimeout={300}
                    transitionLeaveTimeout={300}>
            <Component />
        </ReactCSSTransitionGroup>
    )
} />
        );
    }


class App extends React.Component {

    render() {
        return (

            <Router history={browserHistory}>
                <Switch>
                    <FadeRoute exact path="/" component={Home} />
                    <FadeRoute exact path="/NextComponent" component={NextComponent} />
                    <FadeRoute component={NoMatch} />
                </Switch>
            </Router>

        );
    }

}

ReactDOM.render(
    <App />,
    document.getElementById('root')
);

这里是css代码

.fade-enter {
  opacity: 0.01;
}

.fade-enter.fade-enter-active {
  opacity: 1;
  transition: opacity 500ms ease-in;
}

.fade-leave {
  opacity: 1;
}

.fade-leave.fade-leave-active {
  opacity: 0.01;
  transition: opacity 300ms ease-in;
}

你能帮我找到pb在哪里吗? 提前谢谢你

我已经通过将整个 Switch 包装在一个管理 CSSTransitionGroup 的组件中来设法让它工作。 CSSTransitionGroup 需要同时临时渲染 2 个路由才能在它们之间淡入淡出,所以它需要坐在更高的层次上。

class Fade extends Component {

    render() {
        return (
            <Route render={({location}) => (
                <CSSTransitionGroup
                    transitionName="fade"
                    transitionEnterTimeout={200}
                    transitionLeaveTimeout={200}
                >
                    {React.cloneElement(this.props.children, {location: location, key: location.key})}
                </CSSTransitionGroup>
            )}/>
        );
    }
}


class App extends Component {

    render() {
        return (
            <BrowserRouter>
                <Fade>
                    <Switch>
                        <Route exact path="/" component={Home}/>
                        <Route path="/portfolio" component={Portfolio}/>
                        <Route path="/contact" component={Contact}/>
                        <Route component={NoMatch}/>
                    </Switch>
                </Fade>
            </BrowserRouter>
        );
    }
}

我用类似@RichardY 的解决方案来处理这个问题,我发布了 Component combinating,你可以在那里使用所有的 transition* 参数...

https://github.com/melounek/switch-css-transition-group

编辑:所以你可以这样使用它:

import SwitchCSSTransitionGruoup from 'switch-css-transition-group'
// ...
<SwitchCSSTransitionGruoup
  transitionName="fade"
  transitionEnterTimeout={300}
  transitionLeaveTimeout={300}>
  <Route exact path="/" component={Home}/>
  <Route path="/portfolio" component={Portfolio}/>
  <Route path="/contact" component={Contact}/>
  <Route component={NoMatch}/>
</SwitchCSSTransitionGruoup >