withRouter wrapped component cannot access params when used in NavLink
See original GitHub issueI’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
- Create a component using
withRouter - Use
this.props.match.paramsin that component - 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:
- Created 5 years ago
- Comments:7 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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
pathorurlof the match prop. I haven’t tried using the params. Interestingly, theisExactproperty seems to update correctly for match, but nothing else.A fix would be to forgoe using the match provided by Route in NavLink and instead call matchPath() there directly.
Something like this: (Snippet)
But I’m not entirely sure if that is a viable solution.