Page transitions AND component animation in ReactJS

。_饼干妹妹 提交于 2019-12-02 01:42:20

I've found a solution, but it's not particularly elegant.

In brief, the problem occurred because any components which are rendered by a route technically only ever appear. So what I had to do was use the route's render function, like so:

<CSSTransitionGroup
transitionName="test"
transitionAppear={true}
transitionAppearTimeout={1000}
transitionEnterTimeout={1000}
transitionLeaveTimeout={1000}>

<Route exact path="/about" location={this.props.location} key={this.props.location.key} render={({ location }) => (

    <CSSTransitionGroup
    transitionName="test2"
    transitionAppear={true}
    transitionAppearTimeout={2000}
    transitionEnterTimeout={1000}
    transitionLeaveTimeout={1000}>

        <AboutBox key={this.props.location.key} />

    </CSSTransitionGroup>

)} />

</CSSTransitionGroup>

So what is happening here is:

  • The route is wrapped in a transition group, which means it animates on appear, enter and leave
  • The route itself is NOT using component=, it is using a render function (render=) to call the component (AboutBox)
  • Because this too is wrapped in a transition group, it can be animated on appear, enter and leave

If I were to move that transition group to the component itself, only appear would be available to it.

Hope this helps somebody!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!