RouterContext.push() 在导航时重新加载页面
RouterContext.push() reloads page on navigation
这是我的路由器配置:
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="email" component={EmailPage}>
<Route path="accountinfo" component={AccountInfo}/>
</Route>
</Route>
</Router>
在EmailPage
我有这个方法:
class EmailPage {
onEmailSubmit() {
this.context.router.push('/email/accountinfo');
}
}
我第一次提交时,它会重新加载整个页面并清除 'emailPage' 组件的状态。
使用 React 路由器 2.0.0-rc4。我做错了什么吗?
更新:
我看到 url 发生了变化:
http://localhost:8080/#/email/?_k=1w69uj
to
http://localhost:8080/?#/email/accountinfo?_k=ezdjg6
instead of
http://localhost:8080/#/email/accountinfo?_k=ezdjg6
请注意 #
之前缺少额外的 ?
,这似乎是导致重新加载的原因
我曾经遇到过类似的问题..
以此为例..
getRef(ref) {
this.placeRef = ref;
}
handleSubmit(e) {
e.preventDefault();
console.log(e);
var place = this.placeRef.value;
this.placeRef.value = '';
this.context.router.push('/place/'+place);
}
render() {
return (
<div id="search">
<form onSubmit={(e) => this.handleSubmit(e)}>
<div id="input_place">
<input type="text" className="design" ref={(ref) => this.getRef(ref)} />
</div>
<div id="place_search">
<button type='submit' className="design">Search</button>
</div>
</form>
</div>
);
}
在 handleSubmit()
函数中,如果您删除 e.preventDefault()
,则会重新加载整页。
所以我想这可能是您问题的解决方案。
这是我的路由器配置:
<Router history={hashHistory}>
<Route path="/" component={App}>
<Route path="email" component={EmailPage}>
<Route path="accountinfo" component={AccountInfo}/>
</Route>
</Route>
</Router>
在EmailPage
我有这个方法:
class EmailPage {
onEmailSubmit() {
this.context.router.push('/email/accountinfo');
}
}
我第一次提交时,它会重新加载整个页面并清除 'emailPage' 组件的状态。
使用 React 路由器 2.0.0-rc4。我做错了什么吗?
更新:
我看到 url 发生了变化:
http://localhost:8080/#/email/?_k=1w69uj
to
http://localhost:8080/?#/email/accountinfo?_k=ezdjg6
instead of
http://localhost:8080/#/email/accountinfo?_k=ezdjg6
请注意 #
之前缺少额外的 ?
,这似乎是导致重新加载的原因
我曾经遇到过类似的问题.. 以此为例..
getRef(ref) {
this.placeRef = ref;
}
handleSubmit(e) {
e.preventDefault();
console.log(e);
var place = this.placeRef.value;
this.placeRef.value = '';
this.context.router.push('/place/'+place);
}
render() {
return (
<div id="search">
<form onSubmit={(e) => this.handleSubmit(e)}>
<div id="input_place">
<input type="text" className="design" ref={(ref) => this.getRef(ref)} />
</div>
<div id="place_search">
<button type='submit' className="design">Search</button>
</div>
</form>
</div>
);
}
在 handleSubmit()
函数中,如果您删除 e.preventDefault()
,则会重新加载整页。
所以我想这可能是您问题的解决方案。