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

Time:08-03

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>
  )
}

CodePudding user response:

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
      ))}

  • Related