2

我想为反应路由器创建一个过渡动画,但它似乎不起作用(没有过渡)。

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

这是我的反应代码

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;
}

你能帮我找到铅在哪里吗?先感谢您

4

2 回答 2

1

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

https://github.com/meloonek/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 >
于 2017-07-18T11:44:56.093 回答
1

我已经设法通过将整个 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>
        );
    }
}
于 2017-05-30T09:50:00.993 回答