What to Bear in Mind Before You Buy Homes for Sale in Milton?

Read the essential tips to keep remember before you make a final transaction. Buy homes for sale in Milton with Prosperhill Realty agents.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Redirect with React Router

Two ways to handle redirecting on a user event such as create, update and delete with React Router.

When React Router renders a component, it passes 3 props: history, match and location. What we are interested in here is history.

Using history.push is simple:

this.props.history.push('path')

Example component:

In this example, I am using the Redux pattern so I pass handleSuccess to the addPost action and call it if the POST request is successful.

This is the Route:

We only have access to the history prop in components rendered by React Router. To get access to this prop in a component not rendered by React Router, we can use the withRouter higher-order component.

Add withRouter to the component and use history.push as normal.

Using the <Redirect /> component is arguably more in line with the principles of React.

Once you import the Redirect component from React Router, you can render the <Redirect /> component based on changes to the local state of the component in question.

Here is an example from the same project:

Using history.push requires less code and using. <Redirect /> follows the core principle of React (state change => re-render). Which you use is ultimately up to you.

Add a comment

Related posts:

Flat Bridal Shoes are Your Comfortable Companion On your Wedding Day

If your wedding date is approaching fast, you must be spending busy weekends looking for the perfect outfit and footwear. But it seems like a daunting task. Isn’t it? Especially, looking for that…

6 Reasons to Celebrate National Homeownership Month

Our homes are so much more than the houses we live in. For many, they’ve also become our workplaces, schools for our children, and safe harbors in which we’ve weathered the toughest moments of a…