我使用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;
这不是重定向,我一直在试图弄清楚这一点。我确信有更好的解决方案可用,但由于缺乏知识,我不确定是否实施它。任何帮助表示赞赏。谢谢。
我认为问题在于你正在向重定向添加状态属性,但是基于文档重定向没有状态属性。 https://reacttraining.com/react-router/web/api/Redirect –
是的,谢谢,我改变了大部分基于它的逻辑,但因为这些例子非常简洁。我觉得很困难。 – Annie