question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next.js links don't work inside toasts

See original GitHub issue

🐛 Bug report

In my project I’ve created a Link component that wraps Chakra’s link component with Next.js’ as recommended. This component works fine when I use it in regular places inside my app, but breaks when used inside toasts. On click I get thrown the error “TypeError: can’t access property “push”, router is null”. I think it might have to do with the fact that toasts are placed inside a portal?

toast-portal

💥 Steps to reproduce

  1. Go to the sandbox provided below
  2. Explore implementation of Link component
  3. Test the instances of Link within pages and confirm that they work
  4. Trigger the toast and test that instance of Link
  5. Observe error thrown by Next.js

💻 Link to reproduction

CodeSandbox reproduction: https://codesandbox.io/s/purple-wood-q6ug6

🧐 Expected behavior

I expect this link component pattern to work inside toasts too.

🌍 System information

Software Version(s)
Chakra UI 1.4.1
Browser N/A
Operating System N/A

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mwmcodecommented, May 10, 2021

There is a todo comment in toast.class I think once it’s todo-ed, it might solve this issue

* @todo
*
* Update toast constructor to use `PortalManager`'s node or document.body.
* Once done, we can remove the `zIndex` in `toast.manager.tsx`
0reactions
yukukotanicommented, Aug 7, 2022

@kfranqueiro You can simply avoid this issue using toastOptions.portalProps.containerRef. I think this way is not as complicated as being called “workaround” and this issue can be closed.

sandbox: https://codesandbox.io/s/next-js-links-dont-work-inside-toasts-forked-k0m1o2

function App({ Component, pageProps }: AppProps) {
  const portalContainerRef = useRef();
  return (
    <>
      <div>
        <ChakraProvider
          toastOptions={{ portalProps: { containerRef: portalContainerRef } }}
        >
          <Component {...pageProps} />
        </ChakraProvider>
      </div>
      <div ref={portalContainerRef} />
    </>
  );
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

toast doesn't show correct style - nextjs - Stack Overflow
Here goes my first answer on SO! Ran into a similar situation while trying out React-Toastify as well. Basically you need to state...
Read more >
ToasterContainer breaks Next.js SSR #3430 - GitHub
ToasterContainer breaks server side rendering with Next.js, causing it to show a white page that will only render with JavaScript enabled.
Read more >
Advanced Features: Error Handling - Next.js
Error Handling. This documentation explains how you can handle development, server-side, and client-side errors. Handling Errors in Development.
Read more >
Next.js - Alert (Toaster) Notifications | Jason Watmore's Blog
This tutorial shows how to implement a simple reusable alert component in a Next.js front-end app using React and RxJS.
Read more >
Handling Bootstrap integration with Next.js - LogRocket Blog
js bootstrap 'document is not defined' error. This article will explain how to fix this error as well as how to effectively use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found