Assume I have the following pages on my website
- Page 1:
example.com/somepage.php?action=a
- Page 2:
example.com/somepage.php?action=b
- Page 3:
example.com/somepage.php?action=b&other=c
IMPORTANT NOTE: I do not have control of the URL paths, this is how they are set, and this is why they are far from ideal.
I am creating a React app, and I am trying to set routing, so here is what I started with:
function MyNav () {
return (
<Routes>
<Route path='/somepage.php?action=a' element={<Page1 />} />
<Route path='/somepage.php?action=b' element={<Page2 />} />
<Route path='/somepage.php?action=b&other=c' element={<Page3 />} />
<Route path='*' element={<Page1 />} />
</Routes>
)
}
But this did not work. After investigation, and after looking into the window.location
object, it turned out that path
in <Route/>
will be compared to location.pathname
. So, for all the pages above, the window.location
object will look as follows:
window.location
...
pathname: "/somepage.php"
search: "?action=a" // <-------- this is for Page 1, and will be different for Page 2 and Page 3
...
So, the route for Page 1, should be set as follows
<Routes>
<Route path='/somepage.php' element={<Page1 />} />
...
</Routes>
But this would create a problem, because it will be the same route for all pages, as follows:
<Routes>
<Route path='/somepage.php' element={<Page1 />} />
<Route path='/somepage.php' element={<Page2 />} />
<Route path='/somepage.php' element={<Page3 />} />
<Route path='*' element={<Page1 />} />
</Routes>
Now, to solve the problem above, I found this solution, so I modified my code a little, as follows
function MyNav () {
const getPage = (location) => {
const params = new URL(location.href).searchParams;
const action = params.get('action');
const other = params.get('other');
if (action == 'a') {
return <Page1 />
}
if (action == 'b') {
if (other == 'c') {
return <Page3 />
}
else {
return <Page2 />
}
}
}
return (
<Routes>
<Route path='/somepage.php' element={ getPage (location) } />
<Route path='*' element={<Page1 />} />
</Routes>
)
}
Now this works (yay!), however it defeats the purpose of the path
prop in <Route/>
. So, is there a way to directly search for the search parameters from within <Route/>
? Maybe something like this? (IMPORTANT: THE FOLLOWING IS PSEUDO CODE)
<Routes>
<Route path='/somepage.php' params='action=a' element={<Page1 />} />
<Route path='/somepage.php' params='action=b' element={<Page2 />} />
<Route path='/somepage.php' params='action=b&other=c' element={<Page3 />} />
<Route path='*' element={<Page1 />} />
</Routes>