Basically I have a login route where I wanna be able to declare an optional path parameter.
In my React Router, I have the following path for login:
<Route exact path={routingService.loginUrl(":returnUrl?")} render={Auth} />
Where routingService is declared like this:
const routingService = {
loginUrl: (returnUrl?: string) => `/login/?${encodeURIComponent(returnUrl || "")}`,
};
I tried this way but it didn't work. It redirects me to the NotFound page instead.
<Route path="*" component={NotFound} />
But when I manually change the path to
<Route exact path="/login/:returnUrl?" render={Auth} />
...it does work, but I want to organize the path by using the one (loginUrl) we have in routingService.
This might be really simple to fix, but I'm kinda struggling. Any thoughts?
Thank you!
(returnUrl?: string) => `/login/?${encodeURIComponent(returnUrl || "")}`appends thereturnUrlwhere a queryString would be. Remove the?from the string template. Can you be more specific about what isn't working? Is there an error? You probably don't need to URI encode your route path parameter.routingService.loginUrlnot just return the string"/login/:returnUrl?"for the route'spathvalue? Do you have more than one login route that requires calling the second path segment a different name? Can you provide a more complete/comprehensive code example, or also a running codesandbox demo that reproduces the issue that we can inspect and debug live?