Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm trying to render a component from an object, but the code is failing with an error:

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

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

function AppRouter() {
  return <Routes>
      {publicRoutes.map(({path, component}) => (
        <Route path={path} component={component} /> // warning
      ))}
    </Routes>
  )
}
like image 251
chupapee Avatar asked Sep 06 '25 03:09

chupapee


1 Answers

Since version 6, React Router Dom no longer takes components, but elements instead. [react-router-dom] So instead of passing component you need to pass <Component/> like this:

{publicRoutes.map(({path, component: Component}) => (
  <Route path={path} element={<Component/>} /> // no warning
))}
like image 191
Ahmed Lazhar Avatar answered Sep 07 '25 21:09

Ahmed Lazhar