0

I'm trying to create URL structure that looks like this:

/app?page=home
/app?page=posts
/app?page=post&post_id=123

(instead of /app/home, /app/posts, /app/post/123 ...)

So I do:

<Router basename="/app">
  <div>
    <Link to="?page=home">Home</Link>
    <Link to="?page=posts">Posts</Link>
    <Link to="?page=posts&post_id=123">Post 123</Link>

    <Route path="?page=home" component={Home} />
    <Route path="?page=posts" component={Posts} />
    <Route path="?page=post&post_id=:id" component={Post} />
  </div>
</Router>

But it doesn't seem to work, I can navigate (URL changes), but Route components do not render

Marvin3
  • 4,596
  • 6
  • 28
  • 39
  • 2
    I don't think you can declare path like this in react router. You have to parse the query string and render the component by some conditional logic. – Prakash Sharma May 19 '19 at 07:20
  • Yes those are not paths they are parameters, the answer to this question outlines how to switch between "routes" with query params: https://stackoverflow.com/questions/49724101/react-router-query-param-match – Melbourne2991 May 19 '19 at 09:34

0 Answers0