2017-12-07 8 views
1

私はreactxでサインアップフォームを作成しましたが、期待どおりに動作します。問題は、ユーザーをダッシュ​​ボードにリダイレクトするためにハッシュルートに/をプッシュしようとしましたが、エラーが発生することです。私も次のようにコンテキストでrouterを使用する:私は上記のコードを書かれているRegisterコンポーネントファイルで登録後にリフィックスでリダイレクトする方法は?

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}); 
      }); 
    } 

、私は(コンポーネントクラスの外)ならびに以下のコードを有する:

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

これは何のエラーも与えません。リダイレクトはどのように実装する必要がありますか? react-routerは新しいバージョンでは動作が異なるようです。

バージョン:"react-redux": "^5.0.6"

+0

* * "それはエラーになります" - どのようなエラーが? –

+0

また、React Routerを使用していると仮定すると、あなたはコンテキストを介してルータにアクセスするつもりはないと確信しています - [リダイレクトの仕方に関するドキュメント](https://reacttraining.com/ react-router/web/example/auth-workflow)? –

答えて

2

私は、あなたが以下の方法のいずれかを使用すべきだと思う:
1)を使用して反応し、ルータ - Reduxのを - https://github.com/reacttraining/react-router/tree/master/packages/react-router-redux、あなたが

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

const goTo = (dispatch) => { 
    dispatch(push('/')); 
} 
を使用することができ、アクションや内部のアクションを作成することができます

2)あなたは、いくつかの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> 
    ) 
    } 
関連する問題