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.

withRouter wrapped component cannot access params when used in NavLink

See original GitHub issue

I’ve searched the related issues and I didn’t find this exact bug. Apologies if I missed it.

The issue is that when I try to use a component wrapped with withRouter inside a NavLink, I don’t get the correct match prop. Weirdly, this is only an issue with NavLink. Everything works correctly within a regular Link.

Version

React Router Dom 4.3.1

Test Case

https://gist.github.com/zfletch/28bbaa330756607d40e4c2acf7d980b2

(Uncomment the NavLink code and click on the person 2 to see it break).

Steps to reproduce

  1. Create a component using withRouter
  2. Use this.props.match.params in that component
  3. Place that component in a <NavLink> tag

Expected Behavior

It should work like in any other component (including <Link>). The match and params should be present.

Actual Behavior

match is null in some cases and {} in other cases. This causes incorrect behavior and errors when one of these components is used inside <NavLink>.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ituracommented, Mar 8, 2019

I am seeing something simliar, also on react-router-dom 4.3.1 and 4.4.0-beta.7. In the snippet below, the log will show that the location prop updates as expected, but not the path or url of the match prop. I haven’t tried using the params. Interestingly, the isExact property seems to update correctly for match, but nothing else.

import React from 'react'
import { BrowserRouter, Route, Link, withRouter } from 'react-router-dom'

const Thing = withRouter(({ match, location }) => {
  console.log(match, location)
  return 'darn '
})

const Index = () => 'index'
const About = () => 'about'

export default () => (
  <BrowserRouter>
    <div>
      <Thing />
      <Link to='/'>LinkToIndex </Link>
      <Link to='/about'>LinkToAbout </Link>
      <Route path="/" exact component={Index} />
      <Route path="/about" component={About} />
    </div>
  </BrowserRouter>
)
0reactions
StringEpsiloncommented, Mar 12, 2019

A fix would be to forgoe using the match provided by Route in NavLink and instead call matchPath() there directly.

Something like this: (Snippet)

// NavLink render():
<Route
  location={location}
  children={({ location }) => {
    const match = location.pathname && escapedPath ?
      matchPath(location.pathname, { path: escapedPath, exact, strict}):
      null;
    const isActive = !!(isActiveProp
      ? isActiveProp(match, location)
      : match);

But I’m not entirely sure if that is a viable solution.

Read more comments on GitHub >

github_iconTop Results From Across the Web

withRouter wrapped component cannot access params when ...
The issue is that when I try to use a component wrapped with withRouter inside a NavLink , I don't get the correct...
Read more >
How to pass params into link using React router v6?
Solution · Convert DetailsPage to be a function component and use the useParams react hook. · Write your own withRouter HOC to access...
Read more >
FAQs v6.6.1 - React Router
In React Router v6 we switched from using v5's <Route component> and <Route render> ... but how do I access the router's data,...
Read more >
React Router - Testing Library
If you find yourself adding Router components to your tests a lot, you may want to create a helper function that wraps around...
Read more >
React Router DOM: How to handle routing in web apps
If you find yourself using both, it's OK to get rid of React ... going to import this component from react-router-dom and use...
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