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 12.2 newNextLinkBehavior support

See original GitHub issue

Describe the bug

Next.js 12.2 support new link behavior.

From https://nextjs.org/blog/next-12-2#other-improvements

next/link no longer requires manually adding as a child. You can now opt into this behavior in a backward-compatible way.

https://github.com/vercel/next.js/pull/36436

I added the newNextLinkBehavior flag in my next config in next.config.js

{
  experimental: {
    newNextLinkBehavior: true,
  }
}

but when I use storybook with the new link behavior it seems to not understand it.

I get

Multiple children were passed to <Link> with `href` of `/` but only one child is supported https://nextjs.org/docs/messages/link-multiple-children 

error although in my app this resolves well.

Your minimal, reproducible example

nextjs example with-storybook-app

Steps to reproduce

  1. Add
{
  experimental: {
    newNextLinkBehavior: true,
  }
}

to next.config.js. 2. Create a component with multiple children inside Link like

import Link from "next/link";
export function MenuItem() {
  return (
    <Link href="/" >
      <div/>
      <div/>
    </Link>
  );
}

  1. Use this component in storybook story
 import { ComponentMeta, ComponentStory } from "@storybook/react";
import { MenuItem } from "./MenuItem";

export default {
  component: MenuItem,
} as ComponentMeta<typeof MenuItem>;

const Template: ComponentStory<typeof MenuItem> = (args) => (
  <MenuItem {...args} />
);

export const Regular = Template.bind({});

Expected behavior

No error will occur and the new link behavior will be supported

How often does this bug happen?

Every time

Screenshots or Videos

image

Platform

MacOS Monterey 12.4 Chrome Version 103.0.5060.114 (Official Build) (arm64)

storybook-addon-next version

^1.6.7

Additional context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
B2o5Tcommented, Aug 17, 2022

@nirtamir2 seems like this addon doesn’t pass down experimental options, found a workaround to work with newNextLinkBehavior 💪

in your preview.tsx file add the following

import NextLink, { LinkProps } from 'next/link';

const OriginalNextLink = NextLink;

Object.defineProperty(NextLink, 'default', {
  configurable: true,
  value: (props: LinkProps) => <OriginalNextLink {...props} legacyBehavior={false} />,
});
2reactions
gregorybolkenstijncommented, Sep 1, 2022

@B2o5T Thanks, that fixed it for me!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blog - Next.js 12.2
Next.js 12.2 introduces stable Middleware and On-Demand ISR, experimental Edge SSR and API Routes, and more!
Read more >
Why are /de-DE routes with i18n Next.js returning a 500?
Here's the site: TaxPal - Accounting made simple for small businesses Removing /de-DE from the url works fine It seems to work fine...
Read more >
How to enable NextJS "next/future/image"? - Stack Overflow
json to "next": "^12.2.0" . Here's my next.config.js file: /** @type {import('next ...
Read more >
next: Versions | Openbase
Add support for next.js development in docker: #43138. Credits ... Set __NEXT_NEW_LINK_BEHAVIOR in Jest tests when newNextLinkBehavior is true: #40702 ...
Read more >
12.2.5 - next - npm
next. TypeScript icon, indicating that this package has built-in type declarations. 12.2.5 • Public • Published 4 months ago.
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