Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React router redirect after action redux

I'm using react-redux and react-router. I need to redirect after an action is dispatched.

For example: I have registration a few steps. And after action:

function registerStep1Success(object) {     return {         type: REGISTER_STEP1_SUCCESS,         status: object.status    }; } 

I want to redirect to page with registrationStep2. How can I do this?

p.s. In history browser '/registrationStep2' has not been visited. This page appears only after successful result registrationStep1 page.

like image 335
ximet Avatar asked Feb 29 '16 18:02

ximet


People also ask

How do you redirect after form submit react?

To redirect on form submit using React Router: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function passing it the path - navigate('/contacts') . The navigate() function lets us navigate programmatically.

How do I redirect after Axios Post react?

To make redirects after an axios post request with Express and React, we set window. location to the new URL value after the response from the axios request to the Express back end is available. import React, { Component } from "react"; //... class MyClass extends Component { onSubmit = async (e) => { e.

How do I redirect to another page in react?

To redirect to another page on button click in React: Use the useNavigate() hook, e.g. const navigate = useNavigate(); . Call the navigate() function, passing it the path - navigate('/about') . The navigate() function lets us navigate programmatically.


1 Answers

With React Router 2+, wherever you dispatch the action, you can call browserHistory.push() (or hashHistory.push() if that’s what you use):

import { browserHistory } from 'react-router'  // ... this.props.dispatch(registerStep1Success()) browserHistory.push('/registrationStep2') 

You can do this from async action creators too if that is what you use.

like image 132
Dan Abramov Avatar answered Oct 13 '22 04:10

Dan Abramov