简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM