Reactjs 如何在react typescript中设置HOC中的道具类型?
我有一个简单的withAuth HOC。我正在尝试定义道具的类型Reactjs 如何在react typescript中设置HOC中的道具类型?,reactjs,typescript,higher-order-components,Reactjs,Typescript,Higher Order Components,我有一个简单的withAuth HOC。我正在尝试定义道具的类型 const withAuth = (Component: typeof React.Component) => (role: string) => { return (props) => { const { data, loading } = useGetUser(); if (loading) { return <p>Loading...</p>;
const withAuth = (Component: typeof React.Component) => (role: string) => {
return (props) => {
const { data, loading } = useGetUser();
if (loading) {
return <p>Loading...</p>;
}
if (!data) {
return <Redirect ssr to="/api/v1/login" />;
} else {
if (role && !isAuthorized(data, role)) {
return <Redirect ssr to="/api/v1/login" />;
}
return <Component user={data} loading={loading} {...props} />;
}
};
};
给你:
从“React”导入React,{FC}
类型道具={
名称:string
}
常数A:FC=()=>
const withAuth=(组件:React.ComponentType
)=>(角色:字符串)=>{
返回(道具:P)=>{
返回;
}
};
const result1=withAuth(A)('hello')({label:'hello'})//错误
const result2=withAuth(A)('hello')({name:'hello'})//确定
这里有:
从“React”导入React,{FC}
类型道具={
名称:string
}
常数A:FC=()=>
const withAuth=(组件:React.ComponentType
)=>(角色:字符串)=>{
返回(道具:P)=>{
返回;
}
};
const result1=withAuth(A)('hello')({label:'hello'})//错误
const result2=withAuth(A)('hello')({name:'hello'})//确定
Tryconstwithauth=(组件:React.ReactNode)
?Tryconstwithauth=(组件:React.ReactNode)
?
React.Component<T>
Component: typeof React.Component<T> // Parameter '(Missing)' implicitly has an 'any' type.
props:T // Cannot find name 'T'