Your analysis should be correct, it's the return of the "redirect". You can easily type properties of React functional components with React.FunctionComponent
.
I would recommend to write a little useAccess hook and use that to get a boolean for display reasons or redirect the user with a useEffect. As you redirect to a different page, it shouldn't matter what the component returns.
So I modified your code to this, I made some changes and added comments, let me know if it helps.
import { createContext, FunctionComponent, useEffect, useMemo } from "react";
interface AuthUserInterface {
token: string;
setToken: (value: string) => void;
loggedIn: boolean;
setBrand: (value: any) => void;
}
const AuthUser = createContext<AuthUserInterface | null>(null);
const useAccess = (isBrowser: boolean) => {
const hasAccess = useMemo(
() => accessTokenVAR() && !jwtToken?.roles.toString().includes("admin"),
// TODO: check if that's correct, these variables were not in your answer,
// might need to add/pass them to the hook
[accessTokenVAR, jwtToken]
);
useEffect(() => {
// redirect here if has
if (hasAccess && isBrowser) window.location.replace("www.somewhere.com");
}, [isBrowser, hasAccess]);
return hasAccess;
};
interface AuthUserProviderProps {
isBrowser: boolean;
}
const AuthUserProvider: FunctionComponent<AuthUserProviderProps> = ({
children,
isBrowser
}) => {
const hasAccess = useAccess(isBrowser);
return (
<>
{!hasAccess && <p>"No access"</p>}
{hasAccess && (
<AuthUser.Provider
value={{
// TODO: values not in question, where do they come from
token,
setToken,
loggedIn
}}
>
{children}
</AuthUser.Provider>
)}
</>
);
};
export { AuthUserProvider as default, AuthUser };
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…