2017-06-13 29 views
0

私のAxiosリクエストをAPIに送信して有効な応答を収集すると、自分のホームルートにリダイレクトしたいと思う。見ての通り、Imはコンテキストを使用しようとしていますが、この場合は「コンテキストが定義されていません」というエラーが表示されます。この場合、私は自分のホームルートにどのように移動できますか?私はhistory.pushを使ってみましたが、どちらもうまくいかないようです。どんなアイデアも高く評価されますか?反応ルータを使ってナビゲートする

import React, {Component} from 'react' 
import axios from 'axios' 
import Home from './Home' 
import { 
    BrowserRouter, 
    Link, 
    Route 
} from 'react-router-dom' 

class SignUp extends Component { 

    constructor(props){ 
    super(props); 
    this.state = { 
     email: '', 
     password: '' 
    }; 
    } 

    handleChange = (e) => { 
    this.setState({ 
     [e.target.name]: e.target.value 
    }) 
    } 

    handleClick =() => { 
    axios 
     .post('http://localhost:9000/signup',{ 
     email: this.state.email, 
     password: this.state.password 
     }).then(function(response){ 
     console.log(response.data.success) 
     this.context.router.push('/home'); 
     }) 
    } 
    render(){ 
    return(
     <BrowserRouter> 
     <Route path="/" render={() => (
      <div> 
      <h1> Sign Up</h1> 
      <input name="email" placeholder="enter your email" onChange={e => this.handleChange(e)}/> 
      <br/> 
      <input name="password" placeholder="enter your password" onChange={e => this.handleChange(e)}/> 
      <br/> 
      <button onClick={() => this.handleClick()}>submit</button> 
      <Route exact path="/home" component={Home}/> 
      </div> 
     )}/> 
     </BrowserRouter> 
    ) 
    } 
} 

SignUp.contextTypes = { 
    router: React.PropTypes.func.isRequired 
}; 

export default SignUp 
+0

適切なコンテキストが関数ハンドラに伝達されるように、関数をバインドする必要があります。 'on.handleClick.bind(this)}' 以上を入力すると、 'onClick = {this.handleClick.bind(this)}' – char

+0

入力していただきありがとうございます。これをバインドしましたが、まだコンテキストは定義されていません! !! – hyper0009

+0

あなたは試しました 'react-router'から{browserHistory}をインポートしてください。 browserHistory.push( '/'); –

答えて

0

ルートは、常にすべてのコンポーネント(またはコンテナ)の上にある必要があります。次に、コンポーネントが "inside"ルータ(あなたの場合はBrowserRouter)になると、そのコンテキストにアクセスします。

また、内部にはレンダリング機能があり、意味をなさないものもあります。

だから、このようなものは動作するはずです:

import React, {Component} from 'react' 
import axios from 'axios' 
import Home from './Home' 
import { 
    BrowserRouter, 
    Link, 
    Route 
} from 'react-router-dom' 

class SignUp extends Component { 

    constructor(props){ 
    super(props); 
    this.state = { 
     email: '', 
     password: '' 
    }; 
    } 

    handleChange = (e) => { 
    this.setState({ 
     [e.target.name]: e.target.value 
    }) 
    } 

    handleClick =() => { 
    axios 
    .post('http://localhost:9000/signup',{ 
     email: this.state.email, 
     password: this.state.password 
    }).then(function(response){ 
     console.log(response.data.success) 
     this.context.router.push('/home'); 
    }) 
    } 

    render(){ 
    return(
     <div> 
     <h1> Sign Up</h1> 
     <input name="email" placeholder="enter your email" onChange={e => this.handleChange(e)}/> 
     <br/> 
     <input name="password" placeholder="enter your password" onChange={e => this.handleChange(e)}/> 
     <br/> 
     <button onClick={() => this.handleClick()}>submit</button> 
     </div> 
    ) 
    } 
} 

SignUp.contextTypes = { 
    router: React.PropTypes.func.isRequired 
}; 

class App extends Component { 
    render() { 
    return(
     <BrowserRouter> 
     <Route path="/" component={SignUp} /> 
     <Route exact path="/home" component={Home}/> 
     </BrowserRouter>) 
    } 
} 

export default App; 

そしてもちろん、移動のサインアップ・コンポーネントのスタンドアロンファイルに清潔で、よく構造プロジェクトを維持します。

+0

ありがとうフィードバック!ルートをトップレベルに移動すると、コンテキストが定義されていないという問題が修正されました。私はもうエラーは出ませんが、それは私の家のコンポーネントに私を向けるわけではありません(ボタンをクリックすると同じ '/'ルートに留まります) – hyper0009

+0

実際に私は私の希望を持っていると思います。私はonClick関数を正しく実行していないため、コンテキストがエラーを投げていないのです。 onClickが正しく機能するようになった後も、バインディング後もコンテキストが未定義です。 – hyper0009

関連する問題