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

Cannot read property 'location' of undefined at useLocation #14667

Closed
nemonemi opened this issue Apr 20, 2021 · 5 comments
Closed

Cannot read property 'location' of undefined at useLocation #14667

nemonemi opened this issue Apr 20, 2021 · 5 comments

Comments

@nemonemi
Copy link

Describe the bug
The feature, which worked both in the Storybook and in the application, got the react-router hook useLocation introduced. Since then the feature in the Storybook is broken, but in the application, it works.

To Reproduce
I would provide a codesandbox, but I'm not sure how to set it up for the storybook. Do you have some examples or a boilerplate?

System
Environment Info:
System:
OS: Windows 10 10.0.17763
CPU: (8) x64 Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
Binaries:
Node: 14.16.0 - ~\AppData\Local\Volta\tools\image\node\14.16.0\node.EXE
Yarn: 1.22.10 - ~\AppData\Local\Volta\tools\image\yarn\1.22.10\bin\yarn.CMD
npm: 6.14.11 - ~\AppData\Local\Volta\tools\image\node\14.16.0\npm.CMD
Browsers:
Chrome: 89.0.4389.128
npmPackages:
@storybook/addon-essentials: 6.1.21 => 6.1.21
@storybook/addon-knobs: 6.1.11 => 6.1.11
@storybook/react: 6.1.11 => 6.1.11

@shilman
Copy link
Member

shilman commented Apr 21, 2021

You probably need to provide a decorator that provides the location so that the hook works?

https://storybook.js.org/docs/react/writing-stories/decorators#gatsby-focus-wrapper

@qiuxiaolian
Copy link

qiuxiaolian commented Jun 16, 2021

any example @shilman

@shilman
Copy link
Member

shilman commented Jun 16, 2021

@yukiohori
Copy link

import { MemoryRouter } from "react-router-dom";

export default {
  decorators: [
    (Story) => {
      return (
        <MemoryRouter>
            <Story />
        </MemoryRouter>
      );
    },
  ],
};

something like this works to me

@asifurrahaman754
Copy link

If anyone getting this error:
"You cannot use router inside router", try with this

(Story) => {
            return (
                <UNSAFE_LocationContext.Provider value={null}>
                    <MemoryRouter
                        initialEntries={['path']}
                    >
                        <Story />
                    </MemoryRouter>
                </UNSAFE_LocationContext.Provider>
            );
        },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants