Reactjs React router history.push更新URL但不';与redux一起使用时,不更新组件
我正在为我的应用程序使用react和redux。 我的路线定义如下Reactjs React router history.push更新URL但不';与redux一起使用时,不更新组件,reactjs,redux,react-router,react-redux,Reactjs,Redux,React Router,React Redux,我正在为我的应用程序使用react和redux。 我的路线定义如下 <HashRouter> <div className="container"> <Route path="/register" component={Register} /> <Route path="/login" component={Login} /> </div> </HashRouter> Use
<HashRouter>
<div className="container">
<Route path="/register" component={Register} />
<Route path="/login" component={Login} />
</div>
</HashRouter>
UserService.register(user)
.then(
user => {
dispatch(success());
history.push('/#/login'); //<== here is the issue
},
error => {
dispatch(failure(error));
}
);
现在,注册完成后,我想将用户重定向到login
页面。因此,当登录成功时,我会将他从我的UserAction
功能重定向到login
,如下所示
<HashRouter>
<div className="container">
<Route path="/register" component={Register} />
<Route path="/login" component={Login} />
</div>
</HashRouter>
UserService.register(user)
.then(
user => {
dispatch(success());
history.push('/#/login'); //<== here is the issue
},
error => {
dispatch(failure(error));
}
);
但还是没有运气。有什么解决办法可以解决这个问题吗
注意:我没有使用react router redux您是否尝试过:
this.context.router.push('/page');
至少你能告诉我你的redux路由器版本是什么吗
在您的索引文件中,我建议您:
import routes from './routes'
ReactDOM.render((
<Provider store={store}>
<Router history={hashHistory} routes={routes}/>
</Provider>), document.getElementById('root')
);
从“/routes”导入路由
ReactDOM.render((
),document.getElementById('root'))
);
在路由文件中:
<Route path="/" component={App}>
<IndexRoute component={requireAuth(Home)}/>
<Route path="signup" component={SignUp}/>
</Route>
您的控制台中是否有错误?为什么要使用历史推送更改路由 可以通过
location.hash
方法(因为您使用HashRouter)轻松完成,当然,浏览器和react本身足够智能,可以将其保存在浏览器的历史记录中
因此,无论何时您想要更改url,只需使用以下内容:
location.hash=“/myroute/10/secondroute”
请记住,您可以使用
组件使用react router本身更改url,而不必费心管理哈希或历史记录
链接提供了应用程序周围的声明性、可访问的导航
用法:
从'react router dom'导入{Link}
关于
或
你能试试
history.push('/login')我个人不直接操纵历史。我有条件地呈现一个
组件,并让react router为我做。至少在react router 4.x中,这是用于
just的onClick处理程序,因此切换到链接不太可能解决此问题。
<Link to={{
pathname: '/courses',
search: '?sort=name',
hash: '#the-hash',
state: { fromDashboard: true }
}}/>