Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-router-dom Route error #8210

Closed
nomanabid123 opened this issue Nov 4, 2021 · 6 comments
Closed

react-router-dom Route error #8210

nomanabid123 opened this issue Nov 4, 2021 · 6 comments

Comments

@nomanabid123
Copy link

If we replace <Route path="/" children={}> with

<span path="/" children={}>
it works as Route why?

@nomanabid123
Copy link
Author

nomanabid123 commented Nov 4, 2021

Screenshot (8)

@Grapedge
Copy link

Grapedge commented Nov 4, 2021

It is not an error, in fact, there is no rendering in Route, it is only used to pass props.

export function Route(
  _props: PathRouteProps | LayoutRouteProps | IndexRouteProps
): React.ReactElement | null {
  // do nothing...
}

and in Routes:

export function Routes({
  children,
  location
}: RoutesProps): React.ReactElement | null {
  return useRoutes(createRoutesFromChildren(children), location);
}

createRoutesFromChildren will recursively traverse the child nodes and create routes based on the props of the child nodes, so no matter what node it is, as long as the props are in the shape of RuleObject

export function createRoutesFromChildren(
  children: React.ReactNode
): RouteObject[] {
  let routes: RouteObject[] = [];

  React.Children.forEach(children, element => {
    // ......
    let route: RouteObject = {
      caseSensitive: element.props.caseSensitive,
      element: element.props.element,
      index: element.props.index,
      path: element.props.path
    };
    if (element.props.children) {
      route.children = createRoutesFromChildren(element.props.children);
    }
    routes.push(route);
  });

  return routes;
}

@nomanabid123
Copy link
Author

thank you

@priyeshshah147
Copy link

I am having this error
Error: A is only ever to be used as the child of element, never rendered directly. Please wrap your in a

image

index.js
image

App.js
image

@mjackson
Copy link
Member

mjackson commented Nov 5, 2021

Just FYI we added an invariant that will yell at you about this in #8238

@nomanabid123
Copy link
Author

nomanabid123 commented Nov 8, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants