【问题标题】:ReactCSSTransitionGroup with reactJS 15 and ReactRouter 4ReactCSSTransitionGroup 与 reactJS 15 和 ReactRouter 4
【发布时间】:2017-10-29 18:52:42
【问题描述】:

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

如下所示,我所做的只是创建了一个 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;
}

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

【问题讨论】:

    标签: javascript css reactjs react-router reactcsstransitiongroup


    【解决方案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>
            );
        }
    }
    

    【讨论】:

    • 谢谢,如果我们把所有的开关都包起来,它就可以工作,但我们不能个性化它(改变每条路线的过渡组类型:ex fade, slide ...)
    • 可能不是最优雅的解决方案,但您可以根据 Fade 组件中的 location 属性更改您的 transitionName... 现在想不出更好的方法。
    • 我明白了,例如,通过在 Fade 组件中添加一个开关来检查 location.pathname 并根据情况返回 transitionName,它可以再次成为解决方案
    【解决方案2】:

    我用类似@RichardY 的解决方案来处理这个问题,我发布了 Component 组合,你可以在那里使用所有的 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 >
    

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 2016-03-02
    • 2017-11-15
    • 2018-08-07
    相关资源
    最近更新 更多