Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSX.Element' is not assignable to type 'ReactNode' in React functional HOC

I need to write HOC that will wrap my components into Suspense, I will pass the component and fallback as HOC props.

my hoc:

export const withSuspense = ({ Component, Fallback }: any)=> {

  return (props:any) => {
    return (
      <Suspense fallback={<Fallback />}>
        <Component {...props} />
      </Suspense>
    );
  };
};

Then I need to render in a Route(react-router v 6.15)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  <Route
    path="messages"
    element={withSuspense({MessagesPage,Loader})}
  />

Got an error Type ‘(props: any) => JSX.Element’ is not assignable to type ‘ReactNode’.

I tried to define the HOC props as ReactNodes but it didn’t work. Any ideas, please?

>Solution :

‘JSX.Element’ is not assignable to type ‘ReactNode’ in React functional HOC

element in the newest react-router-dom expects a ReactNode, not a function, that returns JSX or ReactNode.

const Component = withSuspense({ MessagesPage, Loader });

<Route
  path="messages"
  element={<Component />}
/>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading