Skip to content

<Link/> cannot be rendered outside <Router/> but doesn't tell me about that #12989

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

Open
kettanaito opened this issue Feb 10, 2025 · 1 comment

Comments

@kettanaito
Copy link

kettanaito commented Feb 10, 2025

I'm using React Router as a...

library

Reproduction

import { Link } from 'react-router'

<div>
  <Link to="/cart">Go to Cart</Link>
</div>

Sorry for not providing more but it's not necessary, really. The <Link /> component doesn't seem to have the same check as React Router hooks, resulting in a degrading DX.

System Info

I believe this is irrelevant.

Used Package Manager

npm

Expected Behavior

I expect this unintentionally incorrect usage to throw the same error as I get when using React Router hooks without the <Router /> context:

Error: <Link> may be used only in the context of a <Router> component.

This will be a more meaningful error message and will help me figure out what I'm doing wrong, as opposed to the internal "cannot read from context because context doesn't exist", which doesn't tell me much.

Actual Behavior

The following, user-unfriendly error is being thrown:

Cannot destructure property 'basename' of 'React10.useContext(...)' as it is null.

I know this error has been raised before in #11237 and #10867, none of which were actually solved for developers also due to this error being semi-cryptic.

@kettanaito kettanaito added the bug label Feb 10, 2025
@kettanaito
Copy link
Author

If you find my error suggestion appropriate, I can open a pull request with a test and fix for this one. Thanks.

srj-17 added a commit to srj-17/shopping-cart that referenced this issue Mar 11, 2025
Didn't add tests for most of the components, because
it kept showing the error for React10.useContext() returning
null, which seems to be indrectly telling me that <Link />
cannot be used outside of the react router. But the tests
are outside of the react router.

remix-run/react-router#12989

So, I'll add tests for other components Later when I've found
the solutions for this problem
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

2 participants