2017-05-22 237 views
2

我使用ReactJS并且具有形式(组分),其需要重定向到另一个组件中,如果发布请求是成功的。我刚开始使用react路由器,所以这是我尝试重定向的方式。重定向使用反应路由器

import React, { Component } from 'react'; 
import { BrowserRouter as Router, Route, Redirect, Link } from 'react-router-dom'; 
import NextComponent from '/NextComponent'; 
import axios from 'axios'; 

class CurrentComponent extends Component { 

constructor() { 
    super(); 
    this.state = { 
     value: '' 
     redirect: false 
    }; 
} 

handleSubmit() { 
    axios.post('xxx/yyy', { 
     xxx: yyy 
    }) 
    .then(function() { 
     console.log('Success'); 
     this.setState({redirect: true}); 
    }) 
    .catch(function() { 
     console.log('Error'); 
    }); 
} 

render() { 
    return(
     <div> 
     <form> 
      <div className="form-group"> 
      <input type="name" placeholder="name" /> 
      </div> 
      <div className="form-group"> 
      <button type="button" onClick={this.handleSubmit.bind(this)} >Submit</button> 
      {this.state.redirect && 
      <Redirect to={{ 
      pathname: '/nextcomponent', 
      state: {from: this.state.value} 
      }} /> 
      } 
     </div> 
     <Router> 
      <Route path="/nextcomponent" component={NextComponent} /> 
     </Router> 
     </form> 
     </div> 
    ); 
} 
} 

export default PresentComponent; 

这不是重定向,我一直在试图弄清楚这一点。我确信有更好的解决方案可用,但由于缺乏知识,我不确定是否实施它。任何帮助表示赞赏。谢谢。

+0

我认为问题在于你正在向重定向添加状态属性,但是基于文档重定向没有状态属性。 https://reacttraining.com/react-router/web/api/Redirect –

+0

是的,谢谢,我改变了大部分基于它的逻辑,但因为这些例子非常简洁。我觉得很困难。 – Annie

回答

1

也许你没有获得这个职位的呼叫state后,尝试修改handleSubmit方法:

handleSubmit() { 
    let _this = this; 
    axios.post('xxx/yyy', { 
     xxx: yyy 
    }) 
    .then(function() { 
     console.log('Success'); 
     _this.setState({redirect: true}); 
    }) 
    .catch(function() { 
     console.log('Error'); 
    }); 
} 

更新按新代码:

class ComponentOne extends Component { 

    constructor() { 
     super(); 
     this.UpdateRoute= this.UpdateRoute.bind(this); 
     this.state = { 
      value: '' 
     }; 
     this.loggedin = false; 
    } 

    const UpdateRoute =() => (
     <Router> 
     <Route exact path="/xyz" render={() => (
      loggedin ? (
      <ComponentTwo /> 
     ) : (
      <ComponentOne /> 
     ) 
     )}/> 
     </Router> 
    ) 

    handleSubmit() { 
     let _this = this; 
     axios.post('/xyz', { 
      xxx: yyy, 
     }) 
     .then(function(response) { 
      _this.loggedin = true; 
      _this.UpdateRoute(); 
     }) 
     .catch(function() { 
      console.log('Error'); 
     }); 
    } 

    render() { 
     return(
       <div> 
      <h1>Load First</h1> 
      <button type="button" onClick={this.handleSubmit.bind(this)}>Submit</button> 
       </div> 
     ); 
    } 
} 

export default ComponentOne; 
+0

谢谢,但用这种方法我得到了2个组件。我修改了一下代码。你可以在https://gist.github.com/anika-sharma/13c79cb40bd53c6e8f7afca634d9a5fb中看到它。但现在点击登录变量的值不会改变。 – Annie

+0

@安妮检查我更新的答案也尝试看看类似的实施在这里 - https://aggarwalarpit.wordpress.com/2017/05/15/handling-events-in-react/ – Arpit

+0

this.UpdateRoute = this.UpdateRoute.bind(这个);给出一个错误,无法读取undefined的属性绑定。 – Annie

0

这个工作对我的使用情况下,不知道你。试试这个 -

<Route exact path="/some_url" render={() => (
    submitted ? (
    <Redirect to="/profile"/> 
) : (
    <HomePage/> 
) 
)}/> 

修改您的逻辑或将其封装成<Route />就像这个例子。

+0

我想这一点,其路由到新的路径,但同时显示的组件。 – Annie

1

如果你使用的反应路由器4,历史是可以通过上下文:

this.context.history.push('/some/Path'); 

所以,你会修改handleSubmit:

handleSubmit() { 
    axios.post('xxx/yyy', { 
     xxx: yyy 
    }) 
    .then(function() { 
     console.log('Success'); 
     this.context.history.push('/go/somewhere/else'); 
    }) 
    .catch(function() { 
     console.log('Error'); 
    }); 
} 

this answer了改变历史的其他方式没有条件路由。

0

Redirect是的react-routerreact-router-dom出口。因此,你需要将其导入,如:

import { Redirect } from 'react-router' 

Redirect doc

0

使用反应路由器2.8

我使用反应路由器的hashHistory实现这一

import { hashHistory } from 'react-router'; 

又在哪里我想重定向:

hashHistory.push('/go/somewhere/else');