Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js

Redirecting users

What if we want to redirect the user programmatically? Without a click event on a link?

Well, there are couple of ways to do that. The one is through pushing a new url path to history.

But for now we are going to use the <Redirect /> component. First we need to import this component from the react-router-dom library, exactly like the other components at the top of the file. Then we place the component inside some render function of a class component like any other JSX element. and every time this render function is executed we redirect the user to the new URL.

The code above when executed will redirect the user to the /home.

Hint:  Most often, the redirect component is combined with a conditional rendering. So we don't redirect always, but only when something on state changes.