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.

Using functional component as child of <Link/> doesn't work (button)

See original GitHub issue

Bug report

Describe the bug

When using Link (next/Link) with a functional component (button), it stops working.

Basically, the link is ignored. The button’s click is properly triggered, but there is no redirection happening.

To Reproduce

I had the following (working) source code:

<div>
            <Link // TODO sentry warning if < 2k chars https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
              href={`/solutions?${QUERY_SIMULATION_KEY}=${encodeQueryParameter(serializeStudentSolutionContext(studentSolutionContext))}`}
            >
              <Button
                className="btn-border"
                onClick={(): void => {
                  setIsLoadingSolutionsPage(true);
                }}
              >
                {t('chatbotPage.solutionsFound.button', 'Voir les solutions')}
                {
                  isLoadingSolutionsPage ? (
                    <>
                      {' '}<AnimatedBubble />
                    </>
                  ) : null
                }
              </Button>
            </Link>
          </div>

Which I changed into the following (non-working) code:


  const solutionsPageLink = `/solutions?${QUERY_SIMULATION_KEY}=${encodeQueryParameter(serializeStudentSolutionContext(studentSolutionContext))}`; // TODO sentry warning if < 2k chars https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers

  const SolutionsPageButton = (): JSX.Element => {
    return (
      <Button
        className="btn-border"
        onClick={(): void => {
          // Stores the selected persona in the browser localStorage
          patchUserData({ persona: studentSolutionContext.persona });

          // Starts button's animation
          setIsLoadingSolutionsPage(true);
        }}
      >
        {t('chatbotPage.solutionsFound.button', 'Voir les solutions')}
        {
          isLoadingSolutionsPage ? (
            <>
              {' '}<AnimatedBubble />
            </>
          ) : null
        }
      </Button>
    );
  };

// ...

<div>
            {
              // XXX When displayed within an iframe, then open the link in a new page
              //  The point is to avoid displaying too much on the institutional website,
              //  but rather redirect the user to our platform so that they may benefit from all our services
              isInIframe ? (
                <a
                  href={solutionsPageLink}
                  target={'_blank'} // Open in a new tab while keeping the current tab open
                >
                  <SolutionsPageButton />
                </a>
              ) : (
                <Link
                  href={solutionsPageLink}
                >
                  <SolutionsPageButton />
                </Link>
              )
            }
          </div>

Expected behavior

The Link component should redirect in both cases.

System information

  • OS: [e.g. macOS, Windows] Mac
  • Browser (if applies) [e.g. chrome, safari] Chrome
  • Version of Next.js: [e.g. 6.0.2] 9.1

Additional context

Related to https://github.com/zeit/next.js/issues/7915, couldn’t find a solution even though I read https://nextjs.org/docs/api-reference/next/link, but there is not example when using a <Button>.

My easiest workaround would be not using a functional component, and thus duplicate my button’s code.

Alternatively, if the Link component could behave link a <a> (through prop configuration), I wouldn’t have to perform such ternary condition, and could just provide such prop dynamically (no duplicated code, no functional component)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
rafaelalmeidatkcommented, Jan 28, 2020

You are not forwarding the ref in your function component: https://nextjs.org/docs/api-reference/next/link#if-the-child-is-a-function-component

0reactions
balazsorban44commented, Jan 30, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

next/link doesn't work when child is function component #8425
I've tried various methods of adding a ref including forwardRef on my Button component but nothing seems to work. Perhaps I'm doing it...
Read more >
React Link to function isn't working - Stack Overflow
I have a basic React Router setup that I'm trying to get working and I'm really stuck on getting the Link to function...
Read more >
Cleaner components with React Router Hooks - LogRocket Blog
Review three React Hooks that make it easier to work with React Router, a lightweight library for managing routing for React applications.
Read more >
React-Router Hooks - GeeksforGeeks
useParams: This hook returns an object that consists of all the parameters in URL. Syntax: import { useParams } from "react-router-dom"; // ...
Read more >
Test Renderer - React
Create a TestRenderer instance with the passed React element. It doesn't use the real DOM, but it still fully renders the component tree...
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