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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With