ReactJs在状态变量改变时重定向
ReactJs Redirect on state variable change
我试图通过ajax获得json,如果我的响应变量是可接受的使用react路由器重定向。我怎样才能做到呢?
successRedirect(){
if (this.responseCode.equals("what I need")) {
router.transitionTo('/')
}
}
createCheckout() {
$.ajax({
url: "someurl",
dataType: 'json',
type: 'POST',
cache: false,
data: this.data,
success: function(response) {
this.setState({
response: response,
responseCode: response.result.code
});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
})
}
函数必须在获得响应后调用。例如,我有这个代码,并在渲染响应采取并显示一段时间后:
render(){
return (
<div>
<div>Response - {this.state.responseCode}</div>
</div>
);
}
问题似乎出在这一行:
if (this.responseCode.equals("what I need")) {
通过this.setState
添加的项在this.state
对象上可用。此外,JavaScript不提供equals
函数,但您可以与===
进行比较
if (this.state.responseCode === "what I need") {
相关文章:
- 通过javascript重定向html传递php变量
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 使用angular重定向到html页面
- 如何在chrome扩展中重定向到html页面
- 无法从jquery Mobile导航栏重定向到另一个页面
- ng视图外的链接重定向到ng视图内的页面
- 在phonegap应用程序内部重定向不起作用
- CORS-重定向到第二个GET正在接收的页面
- AJAX不会在文件上传后重定向到网页-POST方法
- 重定向时角度刷新浏览器
- 通过链接重定向不;我不在jstree中工作
- 根据select选项元素将表单重定向到不同的URL
- 通过JQuery重定向到另一个页面
- 重定向到“父窗口”
- 改变表单目标,重定向现有iframe
- 重定向页面不改变url
- JS重定向url改变
- 最好的方式来获得一个标签,以改变文本/重定向页面后延迟
- ReactJs在状态变量改变时重定向
- AngularJs会在重定向到新位置时改变后退按钮的位置