Home How to redirect in redux after registration?
Reply: 1

How to redirect in redux after registration?

ALH
1#
ALH Published in 2017-12-07 09:33:44Z

I have created a signup form in react redux and it works as expected. The problem is that I have tried to push / in hash route to redirect user to dashboard, but it gives an error. I even used router in context as below:

if (this.isValid()) {
      // dispatching request for user sing up
      this.props.userSignupRequest(this.state)
          .then(response => {
              this.setState({isLoading: false});
              this.context.router.replaceWith('/');
          })
          .catch(error => {
              this.setState({errors: error.response.data, isLoading: false});
          });
  }

In the Register component file that I have written the above code, I have the below code as well (outside of component class):

Register.contextTypes = {
    router: PropTypes.object.isRequired
};

This does not give any error. How should I implement the redirect? It seems that react-router behaves differently in newer version.

Version: "react-redux": "^5.0.6"

Slawa Eremkin
2#
Slawa Eremkin Reply to 2017-12-07 09:59:18Z

I think, you should use one of the way below:
1) Use react-router-redux - https://github.com/reacttraining/react-router/tree/master/packages/react-router-redux and you can create action and inside action you can use

import {push} from 'react-router-redux'

const goTo = (dispatch) => {
  dispatch(push('/'));
}

2) You can use component to redirect to some url:

  load(){
    if (this.isValid()) {
      // dispatching request for user sing up
      this.props.userSignupRequest(this.state)
          .then(response => {
              this.setState({isLoading: false, goTo: '/'});
          })
    }
  }

  render(){
    const {isLoading, goTo} = this.state;

    return goTo? <Redirect to={goTo} /> : (
       <div>Component</div>
    )
  }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.352773 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO