gpt4 book ai didi

javascript - React Router requireAuth 不重定向

转载 作者:行者123 更新时间:2023-11-28 15:05:05 25 4
gpt4 key购买 nike

我有以下路线和 requireAuth 实现。我有两个问题。

1.) I'm not being redirected from / to /login when I'm not logged in.

2.) The Home component isn't being rendered when I try to access / and I am logged in.

在任何用例中都没有出现控制台错误。我已经确认 IsLoggedIn() 在预期时间返回预期值。所以我的 requireAuth 实现很可能归咎于此。我做错了什么?

index.js

import { isLoggedIn } from './auth';

function requireAuth(nextState, replace, callback) {
if (!isLoggedIn()) {
replace('/login');
}
}

render(
<Provider store={store}>
<Router history={hashHistory}>
<Route path="/" component={Home} onEnter={requireAuth} />
<Route path="/login" component={Login} />
</Router>
</Provider>, document.getElementById('root')
);

auth.js

module.exports = {
isLoggedIn() {
return localStorage.token != undefined;
}
};

最佳答案

If callback is listed as a 3th argument, this hook will run asynchronously, and the transition will block until callback is called.在您的情况下,不需要回调,您可以安全地省略它(因为 isLoggedIn 是同步的)

关于javascript - React Router requireAuth 不重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39336883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com