react typescript Тип для return children: ReactNode cannot be used as a JSX component
Primary tabs
Для children вполне можно использовать тип ReactNode, другое дело если вы возвращаете этот элемент как результат работы функции компонента, то окружите его тэгом, чтобы элемент был единственным напр, нужно писать не так:
import { ReactNode } from 'react'; import { Navigate } from 'react-router-dom'; type ControlledRouteProps = { redirectPath: string, children: ReactNode, isAllowed: boolean, } export default function ControlledRoute({ isAllowed, redirectPath = '/', children }: ControlledRouteProps) { if (!isAllowed) { return <Navigate to={redirectPath} replace />; } return {children}; };
а так:
import { ReactNode } from 'react'; import { Navigate } from 'react-router-dom'; type ControlledRouteProps = { redirectPath: string, children: ReactNode, isAllowed: boolean, } export default function ControlledRoute({ isAllowed, redirectPath = '/', children }: ControlledRouteProps) { if (!isAllowed) { return <Navigate to={redirectPath} replace />; } return <>{children}</>; };
- Log in to post comments
- 92 reads