GoConqr has a new look! Let us know what you think.

Module 6 of 13 · Media

Implement Redirect

Kostas Diakogiannis

Implement Redirect

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.
 

 

Instructions

You must submit an attempt for this quiz to progress. You can submit it after the last question.

Go to the chosen module

Feedback

What is it about?

What do you think of the new look?

How do you like it?