0

I am trying to define a react-router with an optional url parameter prefixed with a namespace. This is an example of such a path:

path="authors/:authorId/posts/:postId?" // application.com/authors/8/posts/4

I want the postId variable part to be optional, but this should include the whole /posts/:postId part to be optional. Is this possible?

Hoetmaaiers
  • 3,413
  • 2
  • 19
  • 29

1 Answers1

0

Not sure why this is a thing that some developers want (haven't seen a good use case scenario where you would need to structure your URL as such), but the simplest solution is to instead just use a query.

http://www.example.com/authors/author?authorId=8&postId=4

Nonetheless, while the answer provided below is specific to the question, the set up for your desired URL structure is the same (see second approach in the answer): React Router v4 Nested match params not accessible at root level

Matt Carlotta
  • 18,972
  • 4
  • 39
  • 51