我有一个带有渲染路由的私有路由组件,Uncaught Invariant Violation: Objects are not valid as a React child (found: [object Promise])当我使用async/await. 我需要改变什么?有一个checkTokenExpirationMiddlewareAdvertiser()可以验证用户的角色并呈现正确的仪表板。似乎当我async/await为用户时,承诺并没有完全解决。
我尝试async从函数中删除 ,但是我无法从函数中获取返回值。
const AdvertiserPrivateRoute = ({ component: Component, ...rest }) => (
<Route
{...rest}
render={props => {
console.log(rest)
if (!loggedInUser())
return (
<Redirect
to={{ pathname: '/login', state: { from: props.location } }}
/>
);
return checkTokenExpirationMiddlewareAdvertiser(
() => <Component {...props} />, // success component
() => <AdvertiserError />, // failure component
);
}}
/>
);
export const checkTokenExpirationMiddlewareAdvertiser = async (success, fail) => {
const { user } = await loggedInUser();
console.log(user)
if (user) {
const { token } = user;
if (jwtDecode(token).exp < Date.now() / 1000) {
removeUser();
return fail();
}
if (user.role !== 'advertiser') return fail();
console.log('here');
return success();
}
console.log("here")
return fail();
};
相关分类