2017-07-28 56 views
0

我正在嘗試重定向到我的reactjs項目的成功場景中的另一個頁面。我已經嘗試了以下方法解釋的方式來做到這一點,但沒有成功重定向到reactjs中的另一個頁面無法正常工作

this.props.router.push('/home'); 

this.context.router.push('/home'); 

this.history.pushState(null, 'home'); 

browserHistory.push('/home'); 

以及在stackoverflow上找到的很多其他方式。我怎樣才能成功地做到這一點。

我的代碼

function handleLoginResponse(response){ 

    switch (response.data.response){ 

     case "200": 
      /** 
      * in sccuess senario 
      * redirect to the home page 
      * set cookies for token and user id create session 
      * */ 

      this.history.pushState(null, 'home'); 


      break; 
     case "201": 
      console.log("201"); 
      break; 
     case "202": 
      console.log("202"); 
      break; 
     case "203": 
      console.log("203"); 
      break; 
     case "204": 
      console.log("204"); 
      break; 
     case "205": 
      console.log("205"); 
      break; 
     case "206": 
      console.log("206"); 
      break; 

    } 

} 

我想重定向到另一個頁面時,我得到的「200」

感謝您的答覆。

+0

什麼是你潰敗成分是什麼樣子? – abdul

+0

您正在使用哪個版本的react-router? –

回答

0

用簡單的方式,您可以使用重定向

import { Redirect } from 'react-router' 

創建狀態然後重定向連接到你的狀態

例子:

import React, {Component} from 'react' 
import { Redirect } from 'react-router' 

export default class MyRedirect extends Component{ 
    constructor(){ 
    super() 
    this.state = { 
     redirect : false 
    } 
    } 

    handleLoginResponse(){ 
    switch(response){ 
     case '200': 
     this.setState({redirect: true}) 
     break; 
    } 
    } 

    render(){ 
    return(
     {this.state.redirect && <Redirect to="yourpath" />} 
    ) 
    } 
}