【发布时间】:2017-08-21 21:40:25
【问题描述】:
我正在尝试制作一个仅在正确完成验证后将用户重定向到新页面的按钮。
有没有办法做这样的事情? 如何在类方法中激活路由?
import validator from './validator';
class Example {
constructor(props) {
super(props)
this.saveAndContinue = thos.saveAndContinue.bind(this)
}
saveAndContinue () {
var valid = validator.validate(this.props.form)
if (valid) {
axios.post('/path')
<Redirect to='/other_tab'>
} else {
validator.showErrors()
}
}
render() {
<button onClick={this.saveAndContinue}>Save and Continue</button>
}
}
【问题讨论】:
-
您应该可以使用
this.props.history.push而不是<Redirect ... /> -
@Purgatory 完全正确,谢谢!
-
我已添加答案供您接受。
标签: reactjs react-router conditional