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

Error: Invariant failed: You should not use <Link> outside a <Router> #8892

Closed
swoppy opened this issue Nov 20, 2019 · 13 comments
Closed

Error: Invariant failed: You should not use <Link> outside a <Router> #8892

swoppy opened this issue Nov 20, 2019 · 13 comments

Comments

@swoppy
Copy link

swoppy commented Nov 20, 2019

Describe the bug
It throws an error when trying to view a components with react-router(s). However, components works fine with the normal react-scripts start

To Reproduce
Steps to reproduce the behavior:
view any components with react-router-dom

Expected behavior
no errors

Code snippets
If applicable, add code samples to help explain your problem.

Screenshots
image

System:
System:
OS: macOS 10.15.1
CPU: (4) x64 Intel(R) Core(TM) i5-5287U CPU @ 2.90GHz
Binaries:
Node: 10.16.2 - /usr/local/bin/node
Yarn: 1.17.3 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
Browsers:
Chrome: 78.0.3904.97
Safari: 13.0.3
npmPackages:
@storybook/addon-actions: ^5.2.4 => 5.2.4
@storybook/addon-links: ^5.2.4 => 5.2.4
@storybook/addons: ^5.2.4 => 5.2.4
@storybook/react: ^5.2.4 => 5.2.4

@swoppy
Copy link
Author

swoppy commented Nov 26, 2019

for the meantime, this lib https://github.com/gvaldambrini/storybook-router mitigated the error

@stale
Copy link

stale bot commented Dec 17, 2019

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Dec 17, 2019
@Hemant27031999
Copy link

Yes, the error still exist. If I try to use the Link component outside a Router, it throws the following error.
Screenshot from 2020-01-13 15-33-37

@stale stale bot removed the inactive label Jan 13, 2020
@mavenik
Copy link

mavenik commented Jan 17, 2020

I'm getting this too. Just bumping the comment thread to not let this go stale.
Meanwhile, https://github.com/gvaldambrini/storybook-router has worked for me.

@afholderman
Copy link
Contributor

afholderman commented Jan 31, 2020

This is not an issue with storybook, the error comes from react-router as is evidenced by the screenshots being posted. If you look at the source of https://github.com/gvaldambrini/storybook-router/blob/master/packages/react/react.js the fix is clear. Wrap the output of your story in a <MemoryRouter/> from react-router. To make this reusable write your own simple decorator:

export const withMemoryRouter = (story: any) => (
  <MemoryRouter>{story()}</MemoryRouter>
);

Storybook can't be expected to pull in and wrap third party library's wrappers. For example similar errors would occur with components that use connect or useSelector outside of a mocked react-redux <Provider/> or @material-ui theme functions outside of a <ThemeProvider/>.

@juanhelbert
Copy link

Same error for me. @afholderman solution worked perfectly.

@stale
Copy link

stale bot commented Feb 29, 2020

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Feb 29, 2020
@ghost
Copy link

ghost commented Mar 7, 2020

Thanks @afholderman

Also we can simply use addDecorator method:

...
import { MemoryRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { store } from '../../../../redux/store/connect';

storiesOf('common', module)
  .addDecorator(getStory => <Provider store={store}>{getStory()}</Provider>)
  .addDecorator(getStory => <MemoryRouter>{getStory()}</MemoryRouter>)
  .add('Component Name', () => <Component/>);

@stale stale bot removed the inactive label Mar 7, 2020
@stale
Copy link

stale bot commented Mar 28, 2020

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Mar 28, 2020
@shilman
Copy link
Member

shilman commented Mar 28, 2020

@nickzcv and for those of you using CSF:

import { MemoryRouter } from 'react-router-dom';
import { Provider } from 'react-redux';
import { store } from '../../../../redux/store/connect';

export default {
  title: 'common',
  decorators: [
    getStory => <Provider store={store}>{getStory()}</Provider>,
    getStory => <MemoryRouter>{getStory()}</MemoryRouter>,
  ]
}

export const ComponentName = () => <Component />;

@stale stale bot removed the inactive label Mar 28, 2020
@stale
Copy link

stale bot commented Apr 19, 2020

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Apr 19, 2020
@stale
Copy link

stale bot commented May 19, 2020

Hey there, it's me again! I am going close this issue to help our maintainers focus on the current development roadmap instead. If the issue mentioned is still a concern, please open a new ticket and mention this old one. Cheers and thanks for using Storybook!

@stale stale bot closed this as completed May 19, 2020
@josh231101
Copy link

I'm using styled components and get this error by creating an styled link, although if I create the styled componentn inside the index.js I get no error, why?

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

7 participants