2017-02-16 12 views
1

フォームを送信した後にpremontessoriページにリダイレクトする必要があります。これどうやってするの?投稿後に前のページにリダイレクトする必要があります

これは私のhandleSubmit機能である:あなたが使用することができます

handleSubmit(event) { 
    event.preventDefault(); 
    <Link to='/premontessori' style={{textDecoration:'none'}} > alert("New Student Created");</Link> 
    var data= { 
      Fname:this.state.Fname, 
      age:this.state.age, 
      Lname:this.state.Lname, 
      dob:this.state.dob, 
      pob:this.state.pob, 
      month:this.state.month, 
      nation:this.state.nation, 
      mothertongue:this.state.mothertongue, 
      bloodgroup:this.state.bloodgroup 
     }; 
     axios.post('/create',data) 
     .then(function(response){ 
     console.log(response); 
     }) 
} 

答えて

0

Importhistory objectの種類は、次のようなhashHistoryまたはbrowserHistoryを使用しています。

import {hashHistory} from 'react-router';

およびリンクは、動的ルーティングのための正しい方法ではない

hashHistory.push('/premontessori');

+0

ありがとうございます。しかし、問題はそれを提出した後にそれがpremontessoriにリダイレクトされていますが、私が提出したものは、premontessoriページに表示されていません。私は今どうすればいい? –

+0

あなたは、そこからデータをリロードするよりも、還元やフラックスのようないくつかのストアにデータを保存することができます。また、hashHistory.push( '/ premontessori?a = b&c = d')やプリプロセッサリコンポーネントの値はthis.props.location.query.aを使用しています –

1

は、移動するためのルータ(例えば-hashHistory)を反応させます。

hashHistory.goBack(); 
1

使用。あなたのコンポーネント名はMyComponentは、コンポーネント

MyComponent.contextTypes = { 
    router: React.PropTypes.object.isRequired 
} 

外に以下の行を追加し、this.props.context.router.push('routename');

私は意志のようにナビゲートされている場合は、以前のルート

に移動しcontext.routerを利用するために必要これを行う前に、axios successコールバック内でこれを行うことをお勧めします。

class MyComponent extends React.Component { 
    .............. 
    handleSubmit(event) { 
      event.preventDefault(); 
      var self = this; 
      var data= {Fname:this.state.Fname, 
       age:this.state.age, 
       Lname:this.state.Lname, 
       dob:this.state.dob, 
       pob:this.state.pob, 
       month:this.state.month, 
       nation:this.state.nation, 
       mothertongue:this.state.mothertongue, 
       bloodgroup:this.state.bloodgroup 
       }; 
      axios.post('/create',data) 
      .then(function(response){ 
      console.log(response); 
      self.context.router.push('/premontessori') 
      }) 
     } 
    .............. 
} 

MyComponent.contextTypes = { 
    router: React.PropTypes.object.isRequired 
} 
+0

コンポーネントの外側ですか? –

+0

編集された答えを確認 –

+0

私はあなたが言ったようにコンポーネントの外にそれらの行を置いたが、それは私にエラーを与えているように定義されていないプロパティ 'contextTypes'を設定できません –

関連する問題