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

Type '() => JSX.Element' is not assignable to type 'ReactNode'

Attempt of calling component from object end in type error:
Type ‘() => JSX.Element’ is not assignable to type ‘ReactNode’

const Login = () => <>login</>

const publicRoutes = [
  {
    path: '/login',
    component: Login
  }
]

function AppRouter() {
  return <Routes>
      {publicRoutes.map(({path, component}) => (
        <Route path={path} element={component} /> // warning
      ))}
    </Routes>
  )
}

>Solution :

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

Since version 6, react routes no longer gets components, but elements instead.
So instead of passing component you need to pass <Component/> like this:

      {publicRoutes.map(({path, component: Component}) => (
        <Route path={path} element={<Component/>} /> // warning
      ))}

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