Test Case
https://codesandbox.io/s/rr00y9w2wm
Steps to reproduce
- Click on Topics
- Click on Rendering with React
OR
Expected Behavior
- match.params.topicIdshould be identical from both the parent Topics component should be the same as- match.params.topicIdwhen accessed within the Topic component
Actual Behavior
- match.params.topicIdwhen accessed within the Topic component is undefined
- match.params.topicIdwhen accessed within the Topics component is rendering
I understand from this closed issue that this is not necessarily a bug.
This requirement is super common among users who want to create a run in the mill web application where a component Topics at a parent level needs to access the match.params.paramId where paramId is a URL param that matches a nested (child) component Topic:
const Topic = ({ match }) => (
  <div>
    <h2>Topic ID param from Topic Components</h2>
    <h3>{match.params.topicId}</h3>
  </div>
);
const Topics = ({ match }) => (
  <div>
    <h2>Topics</h2>
    <h3>{match.params.topicId || "undefined"}</h3>
    <Route path={`${match.url}/:topicId`} component={Topic} />
    ...
  </div>
);
In a generic sense, Topics could be a Drawer or Navigation Menu component and Topic could be any child component, like it is in the application I'm developing. The child component has it's own :topicId param which has it's own (let's say) <Route path="sections/:sectionId" component={Section} />  Route/Component.
Even more painful, the Navigation Menu needn't have a one-to-one relationship with the component tree. Sometimes the items at the root level of the menu (say Topics, Sections etc.) might correspond to a nested structure (Sections is only rendered under a Topic, /topics/:topicId/sections/:sectionId though it has its own normalized list that is available to the user under the title Sections in the Navigation Bar).
Therefore, when Sections is clicked, it should be highlighted, and not both Sections and Topics.
With the sectionId or sections path unavailable to the Navigation Bar component which is at the Root level of the application, it becomes necessary to write hacks like this for such a commonplace use case. 
I am not an expert at all at React Router, so if anyone can venture a proper elegant solution to this use case, I would consider this to be a fruitful endeavor. And by elegant, I mean
- Uses matchand nothistory.location.pathname
- Does not involve hacky approaches like manually parsing the window.location.xxx
- Doesn't use this.props.location.pathname
- Does not use third party libraries like path-to-regexp
- Does not use query params
Other hacks/partial solutions/related questions:
TIA!
 
     
     
     
     
     
    