因此,在我的Reaction应用程序中,我在我的路由组件中使用了专用路由:
const PrivateRoute = ({ children }: { children: ReactNode }) => {
const { session } = useAuth();
return !session ? <Navigate to={"/"} /> : children;
};
UseAuth挂钩:
export const useAuth = () => {
const session = useContext(AuthContext);
if (!session) {
return { session: null, currentUser: null };
}
const currentUser = session.user;
return { session, currentUser };
};
以下是嵌套在专用路由中的仪表板组件的示例:
<Route
path="/dashboard"
element={
<PrivateRoute>
<Dashboard />
</PrivateRoute>
}
/>
现在,在我的仪表板组件中,IM收到错误'currentUser' is possibly 'null'
:
const { currentUser } = useAuth();
const { data: user_assignments } = useGetUserAssignments(currentUser.id);
Why in my private routes components i'm receiving that 100when it clearly only renders components when session is not null?