Yes, I realize this question has been asked, but none of the answers I found resolved this. I am writing a simple higher order component in Typescript to verify authorization before rendering a component. So far it looks like this:
export function withAuth(Component: React.ComponentType) {
if (!Component) return null;
useEffect(() => {
verifyToken().then(res => console.log(res))
}, []);
return (
<Component/>
)
}
I have a larger FunctionComponent called EditorContainer
that I pass to the HOC and export from its own file: export default withAuth(EditorContainer)
;
Which is imported as import EditorContainer from "./modules/Editor/containers/EditorContainer";
and throws this error.
I have tried:
- Passing the HOC a new instance of the component instead of its constructor. This throws a different error.
- Changing or removing all types. The error remains.
- Updating
react, react-dom, @types/react
and@types/react-dom
. - Capitalizing withAuth as WithAuth (I'm running out of ideas here).
- Removing the component from its original location (being rendered by a React Router route). Makes no difference.
It seems like writing a higher order component in TypeScript is disallowed.
` or a `FunctionComponent
`. In your original post, `withAuth` is not an HOC, it is a `FunctionType
` which returns a React Element. `withAuth(EditorContainer)` should return a `ComponentType` to satisfy the contract with react that comes from `export default withAuth(EditorContainer)`. Once you added the anonymous function as the return type of `withAuth`, now you have a valid `ComponentType
– locomotif Aug 25 '19 at 05:57`. The anonymous fn is a valid `ComponentType` because it returns a ReactElement.