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

Storybook - Error: useLocation may be used only in the context of a <Router> component #38

Closed
jakubzet opened this issue Dec 19, 2021 · 2 comments
Labels
question Further information is requested

Comments

@jakubzet
Copy link

jakubzet commented Dec 19, 2021

Hi,
First of all - many thanks for the great package!
I've encountered an issue similar to one in #36. However, as the package update helped in main project, the issue still occurs in Storybook story. It's set up with MemoryRouter - it's the only difference compared to main app.

What I did is - in app, I added the change below:

  // Hook usage
  const location = useLocation();
  const breadcrumbs = useBreadcrumbs(undefined, undefined, location);
  
  // Copied useBreadcrumbs code added locally in app
  const useReactRouterBreadcrumbs = (
    routes?: BreadcrumbsRoute[],
    options?: Options,
    // The location is now added as third parameter
    location?: Location,
  ): BreadcrumbData[] =>
    getBreadcrumbs({
      routes: routes || [],
      // Using provided location makes the error mentioned in title gone
      location: location || useLocation(),
      options,
    });

As you can see, when I use "useLocation" explicitly and pass the results as third, additional parameter, the hook works fine both in app, as well in Storybook.

I can submit a small PR for this change, yet I'm not sure if anyone else had similar issue and if this even the best fix possible.

EDIT: What could be important is that I use new, V6 useRoutes API (as described in docs: https://reactrouter.com/docs/en/v6/getting-started/concepts#defining-routes)

@icd2k3
Copy link
Owner

icd2k3 commented Dec 21, 2021

hi @jakubzet - Can you post an example of your storybook story? I think the original error Error: useLocation may be used only in the context of a <Router> is because <Router> is a provider that useLocation needs. So maybe you'd have to wrap the entire story with it?

I'm doing something similar here in the test file by importing the MemoryRouter then making sure it wraps every test render.

@icd2k3 icd2k3 added the question Further information is requested label Dec 21, 2021
@icd2k3
Copy link
Owner

icd2k3 commented Jan 11, 2022

Closing for now

@icd2k3 icd2k3 closed this as completed Jan 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants