【发布时间】:2017-10-22 10:19:09
【问题描述】:
如果发布请求成功,我正在使用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;
它没有重定向,我一直在尝试解决这个问题。我确信有更好的解决方案可用,但由于我缺乏知识,我不确定是否实施它。任何帮助表示赞赏。谢谢。
【问题讨论】:
-
我认为问题在于您正在向重定向添加状态道具,但根据文档,重定向没有状态道具。 reacttraining.com/react-router/web/api/Redirect
-
是的,谢谢,我已经更改了大部分基于它的逻辑,但是因为示例非常简洁。我觉得很难。
标签: javascript reactjs redirect react-router