Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

router.events.emit('routeChangeError'); is not working #70

Closed
nino-cast opened this issue Feb 22, 2022 · 7 comments
Closed

router.events.emit('routeChangeError'); is not working #70

nino-cast opened this issue Feb 22, 2022 · 7 comments

Comments

@nino-cast
Copy link

image

"next": "12.0.10",
"nextjs-progressbar": "^0.0.13",
// app.tsx

...
<NextNProgress stopDelayMs={0} options={{ showSpinner: false }} />
// form guard

import { useRouter } from 'next/router';
import { useEffect } from 'react';

export const useFormGuard = (isDirty: boolean) => {
  const router = useRouter();
  const message = 'ok?';

  const pageChangeHandler = () => {
    const answer = window.confirm(message);
    if (!answer) {
      // crash here
      router.events.emit('routeChangeError');
      // throw 'routeChange aborted.';
    }
  };

  const beforeUnloadhandler = (event: BeforeUnloadEvent) => {
    event.preventDefault();
    event.returnValue = message;
  };

  useEffect(() => {
    if (isDirty) {
      router.events.on('routeChangeStart', pageChangeHandler);
      window.addEventListener('beforeunload', beforeUnloadhandler);
      return () => {
        router.events.off('routeChangeStart', pageChangeHandler);
        window.removeEventListener('beforeunload', beforeUnloadhandler);
      };
    }
  }, [isDirty, router]);
};
@apal21
Copy link
Owner

apal21 commented Feb 26, 2022

Is this related to this package? If so could you please share codesandbox something similar?

@imranbarbhuiya
Copy link
Collaborator

imranbarbhuiya commented Feb 27, 2022

This isn't related to this package but ik the solution. You are emitting routeChangeError without any arguments. If you check
https://nextjs.org/docs/api-reference/next/router

you'll find routeChangeError emits with 3 args.

routeChangeError(err, url, { shallow }) - Fires when there's an error when changing routes, or a route load is cancelled.

So you need to provide error, url and an object with shallow property.

btw I'm saying to @apal21 that our routeChangeError's handler is set to routeChangeEnd which accepts only url and {shallow} and routeChangeError emits with 3 args and 1st one is error. How it's working? I'll test it later

@nino-cast
Copy link
Author

nino-cast commented Mar 8, 2022

@apal21 @imranbarbhuiya
Thank you for your reply. This is demo.

As @imranbarbhuiya points out, I think the cause of the error is the lack of arguments in the handler for routerChangeError.
https://github.com/apal21/nextjs-progressbar/blob/master/src/index.tsx#L87-L92

The documentation has a shallow as the third error event, but the library handler is trying to get the shallow from the second argument.

@imranbarbhuiya
Copy link
Collaborator

imranbarbhuiya commented Mar 8, 2022

Using {shallow} in url is incorrect. But this wont throw any error bc url.shallow is undefined so it'll assume it as false which will work fine but it needs a fix though. But in your case this isn't the issue. You aren't passing any parameters when using emit which is causing the error.

You should do like this

router.events.emit('routeChangeError',err, yoururl, {shallow})

@nino-cast
Copy link
Author

nino-cast commented Mar 8, 2022

@imranbarbhuiya
thank you for your reply.

I understand that the argument is for listen, not emit. When passing the argument with emit, what kind of content should be specifically passed?

Upon investigation, I could not find an example of passing an argument with emit.

I fork and fix the event handler and try.

@apal21
If you know of Sandox where router.events.emit ('routeChangeError'); works for this library, please let me know.

@imranbarbhuiya
Copy link
Collaborator

this can help maybe

const pageChangeHandler = (
    url: string,
    { shallow }: { shallow: boolean }
  ) => {
    if (!shallow) {
      const answer = window.confirm(message);
      if (!answer) {
        router.events.emit('routeChangeError','routeChange aborted.', url, {shallow});
        throw 'routeChange aborted.';
      }
    }
  };

@nino-cast
Copy link
Author

@imranbarbhuiya
solved. Thank you for your kind answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants