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

Next/Link isn't working. #172

Closed
meotimdihia opened this issue Jul 6, 2021 · 3 comments
Closed

Next/Link isn't working. #172

meotimdihia opened this issue Jul 6, 2021 · 3 comments

Comments

@meotimdihia
Copy link

meotimdihia commented Jul 6, 2021

Next/Link isn't working. This is an example:

https://codesandbox.io/s/next-sweetalert2-bug-example-3rx84

chrome_2021-07-07_01-05-02

Related with the issue: vercel/next.js#16864

I don't really know If this is package or next.js problem.

@zenflow
Copy link
Member

zenflow commented Jul 7, 2021

@meotimdihia You can't use the Next/Link component in Sweetalert2 React content because that content is not part of the Next.js page (element tree). The Next.js page is rendered in the context of the Next.js router, but the Sweetalert2 React content is not. The Next/Link component relies on having the Next.js router in context.

I suggest that instead of using Next/Link you handle the <a> element's onClick event by calling the Router.push() method:

import Router from "next/router";
<a
  href="/day"
  onClick={(e) => {
    e.preventDefault();
    Router.push("/day");
    MySwal.close();
  }}
  className="font-semibold link"
>

https://codesandbox.io/s/next-sweetalert2-bug-example-forked-df89o?file=/pages/index.js

Of course to make things DRY and to separate concerns you could make a component for this.

@zenflow
Copy link
Member

zenflow commented Jul 7, 2021

Closing because there's no action that can be taken in this project.

That said, it would be great if Next/Link used the singleton Router directly instead of getting it from context. (But there may be some reason for that)

@zenflow zenflow closed this as completed Jul 7, 2021
@meotimdihia
Copy link
Author

Thanks for the solution. I am fine with using the next/router.

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

2 participants