React-router: Get params outside of a component

Created on 2 Feb 2016  路  3Comments  路  Source: ReactTraining/react-router

I would like to retrieve the current params outside of a component, and as far as I can tell React Router does not provide a convenient way of doing that.

Sometime back before 0.13 the router had getCurrentParams() which is what I used to use.

Now the best thing I can figure out is:

// Copy and past contents of PatternUtils into my project
var PatternUtils = require('<copy of PatternUtils.js>')

const { remainingPathname, paramNames, paramValues } =
   PatternUtils.matchPattern(
       "<copy of path pattern with params I am interested in>",    
   window.location.pathname);

This is pretty ugly, and I was hoping for a more elegant solution.

All 3 comments

Thanks for your question!

We want to make sure that the GitHub issue tracker remains the best place to track bug reports and feature requests that affect the development of React Router.

Questions like yours deserve a purpose-built Q&A forum. Could you post this question to Stack Overflow with the tag #react-router? https://stackoverflow.com/questions/ask?tags=react-router

We also have an active and helpful React Router community on Reactiflux, which is a great place to get fast help with React Router and with the rest of the React ecosystem. You can join at https://discord.gg/0ZcbPKXt5bYaNQ46.

I found the question that the issue opener posted: https://stackoverflow.com/questions/35160459/get-params-outside-of-a-component. However, there's no response after 2 weeks and I'd like to be able to access params outside of components as well. I'm finding a lot of info on doing this with react-router verion < 2.0, but none with 2.0.

After some more digging, this appears to be an open issue (#2547) and is unresolved so far.

Was this page helpful?
0 / 5 - 0 ratings