'AuthProvider' 不能用作 JSX 组件
'AuthProvider' cannot be used as a JSX component
我尝试在 Context Provider 中进行身份验证,但在使用 TypeScript 时遇到了问题。
这是提供程序代码的问题片段:
interface AuthUserProviderProps {
children?: React.ReactNode
}
interface AuthUserInterface {
token: string;
setToken: (value: string) => void;
loggedIn: boolean;
};
setBrand: (value: {}) => void;
}
export const authUser = createContext<AuthUserInterface | null >(null);
const AuthUserProvider = ({ children }: AuthUserProviderProps) => {
if (accessTokenVAR() && !jwtToken?.roles.toString().includes('admin')) {
return isBrowser ? window.location.replace('www.somewhere.com') : ''
}
return (
<authUser.Provider
value={{
token,
setToken,
loggedIn,
}}
>
{ children}
</authUser.Provider>
);
};
export default AuthUserProvider;
App.tsx
export default function App({ Component, pageProps }: AppProps) {
const apolloClient = useApollo(pageProps.initialApolloState);
return (
<ThemeProvider theme={TifTheme}>
<AuthUserProvider> <===This line throws error
<ApolloProvider client={apolloClient} >
<CssBaseline />
<Component {...pageProps} />
</ApolloProvider>
</AuthUserProvider>
</ThemeProvider>
);
}
错误是'AuthUserProvider'不能用作JSX组件。它的 return 类型 'void | "" | Element' 不是有效的 JSX 元素。类型 'void' 不可分配给类型 'Element.',它是由 return 重定向引起的。
但是我不知道如何处理这个错误
您的分析应该是正确的,是“重定向”的return。您可以使用 React.FunctionComponent
.
轻松键入 React 功能组件的属性
我建议编写一个小的 useAccess 挂钩,并使用它来获取用于显示的布尔值或使用 useEffect 重定向用户。当您重定向到不同的页面时,组件 return 是什么无关紧要。
所以我修改了你的代码,我做了一些修改并添加了评论,如果有帮助请告诉我。
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 };
我尝试在 Context Provider 中进行身份验证,但在使用 TypeScript 时遇到了问题。 这是提供程序代码的问题片段:
interface AuthUserProviderProps {
children?: React.ReactNode
}
interface AuthUserInterface {
token: string;
setToken: (value: string) => void;
loggedIn: boolean;
};
setBrand: (value: {}) => void;
}
export const authUser = createContext<AuthUserInterface | null >(null);
const AuthUserProvider = ({ children }: AuthUserProviderProps) => {
if (accessTokenVAR() && !jwtToken?.roles.toString().includes('admin')) {
return isBrowser ? window.location.replace('www.somewhere.com') : ''
}
return (
<authUser.Provider
value={{
token,
setToken,
loggedIn,
}}
>
{ children}
</authUser.Provider>
);
};
export default AuthUserProvider;
App.tsx
export default function App({ Component, pageProps }: AppProps) {
const apolloClient = useApollo(pageProps.initialApolloState);
return (
<ThemeProvider theme={TifTheme}>
<AuthUserProvider> <===This line throws error
<ApolloProvider client={apolloClient} >
<CssBaseline />
<Component {...pageProps} />
</ApolloProvider>
</AuthUserProvider>
</ThemeProvider>
);
}
错误是'AuthUserProvider'不能用作JSX组件。它的 return 类型 'void | "" | Element' 不是有效的 JSX 元素。类型 'void' 不可分配给类型 'Element.',它是由 return 重定向引起的。 但是我不知道如何处理这个错误
您的分析应该是正确的,是“重定向”的return。您可以使用 React.FunctionComponent
.
我建议编写一个小的 useAccess 挂钩,并使用它来获取用于显示的布尔值或使用 useEffect 重定向用户。当您重定向到不同的页面时,组件 return 是什么无关紧要。 所以我修改了你的代码,我做了一些修改并添加了评论,如果有帮助请告诉我。
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 };