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.

`router is null` - How to lift router provider higher in the component tree?

See original GitHub issue

When running Storybook, I’m getting the error Uncaught TypeError: router is null presumably because I am using useRouter in a provider that’s rendering higher in the tree than RouterContext.Provider

My preview.js looks like this

const AppDecorator = (storyFn) => <RootProviders>{storyFn()}</RootProviders>;

export const decorators = [mswDecorator, AppDecorator];

export const parameters = {
  nextRouter: {
    Provider: RouterContext.Provider,
  },
//.....

Is there a way to render the Next router before any other providers? If not, this maybe should be considered a bug because as you’ll see from the screenshots below, in a regular build the router is rendered above providers in the tree.

Regular app tree Screen Shot 2022-03-04 at 9 48 20 PM

Storybook tree Screen Shot 2022-03-04 at 8 20 42 PM

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:8
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
TomFreudenbergcommented, Apr 6, 2022

In my case I had to inject the WithNextRouter decorator after my AppDecorator.

So I dropped that from main.js

module.exports = {
  ...config,
  addons: [
    ...your addons
    // "storybook-addon-next-router", do not use it here
  ],
};

and brought it into in preview.js

...

import { RouterContext } from "next/dist/shared/lib/router-context";
import { decorators as RouterDecorators } from 'storybook-addon-next-router/dist/preset/addDecorator';


const withAppProviders = (Story, session, context) => (
  <SessionProvider session={session}>
    <AppContextProviders>
      <LocaleContextProviders>
        <ThemeContextProvider>
          <Story {...context} />
        </ThemeContextProvider>
      </LocaleContextProviders>
    </AppContextProviders>
  </SessionProvider>
);

export const decorators = [withAppProviders, ...RouterDecorators];

...

export const parameters = {
  nextRouter: {
    locale: i18n.locales[0],
    Provider: RouterContext.Provider
  },

...

That did the magic on my side

1reaction
TomFreudenbergcommented, Apr 6, 2022

@hisapy

doing like me should help to prevent importing that to ANY story


Hey Aaron @lifeiscontent

maybe you allow ordering / priorisation of presets or put that in documentation?

In any case thanks for your component!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · lifeiscontent/storybook-addon-next-router - GitHub
router is null - How to lift router provider higher in the component tree ? ... ProTip! Updated in the last three days:...
Read more >
useNavigate() may be used only in the context of a <Router ...
This error throws in useNavigate. useInRouterContext will check if the component(which uses useNavigate hook) is a descendant of a <Router> ...
Read more >
Router - Angular
By default, the router re-uses a component instance when it re-navigates to the same component type without visiting a different component first.
Read more >
React Suspense in Practice | CSS-Tricks
This post is about understanding how Suspense works, what it does, and seeing how it can integrate into a real web app. We'll...
Read more >
How To Debug React Components Using React Developer ...
The React Developer Tools browser extension gives you an interface for exploring the React component tree of your JavaScript app, ...
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