Hey, it’s almost I forgot I had a blog… Sorry private life got ridiculously busy.
Let’s talk about React Router v4 and its params in the URL.
It might occur that you need to check them. For instance, for my small webapp
HowWindy.com, I need to make sure that the country
is valid and then that the area
shortcode is valid too. If any of these two is not, I redirect to not found page.
Note: This example is a very simplified version of my existing code as I was using Redux to update some store state but it would just be noise for this example.
Router config
Let’s have a look at my router config:
As you can see, my route is not rendering directly my <ListContainer>
but it goes through an high order component (HOC) called <ValidatedRoute>
The High Order Component
Let’s have a look at the code
ValidatedRoute
is rendered first. That way we can catch “on the fly” the
params and make sure they match our conditions and if not we simply render a
Redirect
component from react-router-dom
to redirect to a not found url.
Solution inspired by this thread on Stackoverflow.