I have a set of dynamically parameters in the URL, ad example the user locale, that looks like that:
/en/homepage
In my router config JSON file, I have something like:
/:locale/homepage
Which is the best way in order to replace these parameters directly in React Router?
I've come up with this solution that looks to me very far away from a standard or portable solution:
const urlTemplate = '/:language/homepage';
const mappedUrl = pathToRegexp.compile(urlTemplate);
const url = mappedUrl({
'language': this.props.match.params.language
})
this.props.history.push(url);
I'm able to retrieve the match parameters if the component is wrapped by withRouter ( HOC from React router ) and replace them with pathToRegexp ( pillarjs/path-to-regexp ), but if I need to use <Link to={}>
in a stateless component it's very annoying.
Are there standard solutions in order to do that?
Thank you in advance :)