繁体   English   中英

react-router-dom:历史不会自动重新加载(渲染)页面

[英]react-router-dom: history does not reload (render) page automatically

我正在使用 redux、redux-saga 以及其他依赖项,我试图在登录后使用 history.push() 重定向页面,但它不起作用。 所以,为了解决这个问题,我在那之后使用了 window.location.reload () 。

这是我的代码的一部分:

 export function* signIn({ payload }) { try { const { email, password } = payload; const response = yield call(api.post, 'sessions', { email, password }); const { token, user } = response.data; yield put(signInSuccess(token, user)); history.push('/home'); window.location.reload(); } catch(err) { yield put(signFailure()); } }

还有我的 App.js:

 export default function App() { return ( <Provider store={store}> <PersistGate persistor={persistor}> <Router history={history}> <Routes /> <ToastContainer autoClose={4000}/> </Router> </PersistGate> </Provider> ); }

为什么会发生? 我读到这是因为在路由器/历史的最新版本中,历史没有像以前那样工作,它不会自动重定向,我不知道,我想这就是我所理解的。

有谁知道如何解决这个问题? 不必每次都使用 window.location.reload()?

对不起我的英语,我来自巴西。

您没有在signIn传奇中使用 React Router history ,因此它不会使用路由器重定向。 由于传奇无法做到这一点,因此您必须将其移动到调用signIn的组件中。

我通常会在signIn中添加一个回调,在调用signIn的组件内部,可以在成功调用后使用history重定向。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM