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.

[QoL] Warn on missing react-router context

See original GitHub issue

When testing one of my components that uses the useReactRouter hook, I got the following error:

Cannot destructure property `history` of 'undefined' or 'null'.

      19 |   active
      20 | }) => {
    > 21 |   const { history } = useReactRouter();
         |                                     ^
      22 |
      23 |   const classes = active
      24 |     ? 'slider__item slider__item--active'

The error itself is a bit ambiguous as it originates from the hook not being able to find the React-Router context. Perhaps a nice addition would be to warn/error on not being able to find the React-Router context.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
konsumercommented, Jan 11, 2019

I am getting an error:

Error: use-react-router may only be used within a react-router context.

Using next versions of react-router, react, & react-dom.

here is a codesandbox.

0reactions
konsumercommented, Jan 11, 2019

Hmm. In the codesandbox it’s not working (same problem that I originally had), but if I download it and install/run it locally it does. I used next in package.json, and pulled use-react-router out of the app when it wasn’t working, so I’m not sure what version it was using. Now, locally it’s 4.4.0-beta.6 (check package-lock) and that seems to work ok, so I will give it another try in the original app.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot unit test components which use <Link /> due ... - GitHub
The problem is that in this case BrowserRouter must be imported from react-router-dom package, which we don't need in a Remix app.
Read more >
Authentication with react context and react router v6 problem
I'm trying to build an authentication service using react context with react router v6 routes and I can´t go to the expected route....
Read more >
Context - React
Context provides a way to pass data through the component tree without having to pass props down manually at every level. In a...
Read more >
Route render prop - React Router v5
The “react-empty” comments are just implementation details of React's null rendering. But for our purposes, it is instructive. A Route is always technically...
Read more >
Complete guide to authentication with React Router v6
The <Route /> component can be thought of like an if statement; it will act upon a URL location with its element only...
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