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

Module 8 of 13 · Media

Redirect after 5 seconds solution

Kostas Diakogiannis

Redirect after 5 seconds solution

Redirecting with lifecycle solution

The answer can be found to the code above.

First inside the render function of the component, we inject the redirect to  /home, but take care, this will be executed only when the shouldRedirect variable of component's state is set to true.

This is initialized as false, but then every time this component renders, we set a timeout function, and after 5 seconds we change a state's variable. We use the componentDidMount lifecycle method for achieving this.

The change of state will execute the render function with the new variables' values, and this will trigger the redirect component, which will lead the user to the /home page.

 

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?