简体   繁体   中英

Transitions are firing too early when using _app.js page component in Next.JS 6

I am trying to set up a relatively simple page transition within Next JS. I am using GSAP TweenLite as my animation library, and I am react-transition-group to handle the transitions, and I am trying to do all of this in the _app.js component that was introduced in Next.js 6.0. I have it "sort of" working, but it's not doing exactly what I want it to do.

The issue that I am having is that when a new route is hit, the page component for that route immediately transitions in at the top of the DOM and the Exiting component gets pushed to the bottom of the page until it transitions out and unmounts.

What I am wanting it to do is transition and unmount the current page component, and then transition and mount the new page component when the route changes. If anyone has any advise on how I could set this up better I would appreciate it greatly.

//GSAP Animations
const fadeIn = node => {
  TweenLite.set(node, {
    opacity: 0
  });
  TweenLite.to(node, 1, {
    opacity: 1,
    ease: Power2.easeInOut
  });
};

const fadeOut = node => {
  TweenLite.set(node, {
    opacity: 1,
    scale: 1
  });
  TweenLite.to(node, 1, {
    opacity: 0,
    scale: 0.5,
    ease: Power2.easeInOut
  });
}; 

export default class MyApp extends App {
  static async getInitialProps({ Component, router, ctx }) {
    let pageProps = {};

    if (Component.getInitialProps) {
      pageProps = await Component.getInitialProps(ctx);
    }

    return { pageProps };
  }


  render() {
    const { Component, pageProps, style } = this.props;
    return (
      <Container>
          <Layout>
            <TransitionGroup>
              <Transition
                key={this.props.router.pathname}
                timeout={1000}
                in={true}
                onEnter={fadeIn}
                onExit={fadeOut}
                mountOnEnter={true}
                unmountOnExit={true}
              >
                <Component {...pageProps} />
              </Transition>
            </TransitionGroup>
          </Layout>
      </Container>
    );
  }
}

Whilst I don't have the exact answer to your question, hopefully reviewing the code in the example below will help you resolve your issue.

https://github.com/xavczen/nextjs-page-transitions

Another approach would be to use the following module:

https://github.com/illinois/next-page-transitions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM