Router Reagent 4 Beta 2 with ReactCSSTransitionGroup

I prefer ReactCSSTransitionGroupfor react-motion. The code below makes the component disappear (appears) when the route changes, BUT the problem: the outgoing component does not disappear, it leaves instantly.

<Switch>
    <FadeRoute exact path="/" component={PageLanding}/>
    <FadeRoute path="/login" component={PageLogin}/>
    <FadeRoute path="/signup" component={PageSignup}/>
    <FadeRoute component={Page404}/>
</Switch>

function FadeRoute({component:Component, ...rest}) {
    return (
        <Route {...rest} children={({location,match}) => (
                <ReactCSSTransitionGroup {...{key:Date.now(), transitionName:'fade', transitionAppear:true, transitionEnter:true, transitionLeave:true, transitionAppearTimeout:300, transitionEnterTimeout:300, transitionLeaveTimeout:300})}>
                    <Component/>
                </ReactCSSTransitionGroup>
        )} />
    );
}
<style>
.fade-enter, .fade-appear { opacity:0; }
.fade-enter.fade-enter-active,
.fade-appear.fade-appear-active { opacity:1; transition: opacity 300ms; }
.fade-leave { opacity:1; }
.fade-leave.fade-leave-active { opacity:0; transition: opacity 300ms; }
</style>
+4
source share
1 answer

The way it works <Switch>is that it only displays the first one <Route>, whose path matches the current location. This means that when navigating, an existing one is <FadeRoute>immediately disabled when navigating. Its child is <CSSTransitionGroup>also disabled, so it does not have the ability to trigger a vacation transition for its children.

, <Switch> <CSSTransitionGroup>. <Switch> - , , key. location, <Switch> location .

<CSSTransitionGroup
  transitionName='fade'
  transitionEnterTimeout={500}
  transitionLeaveTimeout={500}
>
  <Switch key={location.pathname} location={location}>
    <Route path="/red" render={Red} />
    <Route path="/green" render={Green} />
    <Route path="/blue" render={Blue} />
  </Switch>
</CSSTransitionGroup>
+13

Source: https://habr.com/ru/post/1668524/


All Articles