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

Clarify why and how nested routing #841

Open
gxxcastillo opened this issue Aug 7, 2024 · 4 comments
Open

Clarify why and how nested routing #841

gxxcastillo opened this issue Aug 7, 2024 · 4 comments
Labels
help wanted Looking for assistance on this issue solid-router Related to Solid Router

Comments

@gxxcastillo
Copy link

Describe the bug

I'm not able to render my UI when I set up a route config with nested routes using solid-start.

Following documentation from here

Your Example Website or App

https://stackblitz.com/edit/github-aqy729

Steps to Reproduce the Bug or Issue

  1. load the site
  2. navigate to "/" and confirm that nothing renders
  3. navigate to "/Home" and confirm that "Root" renders
  4. navigate to "/View" and confirm that "Root" renders

Expected behavior

I would expect for:

  • "/" to render the text "Root"
  • "/Home" to render the text "Home"
  • "/View" to render the text "View"

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 127

Additional context

No response

@ryansolid
Copy link
Member

The docs could be clearer on a few points I think:

  • When do you need nested routes
  • How do you author parent routes (not just configure them)
  • Index route matching

First what you described doesn't sound like nested routes. It sounds like you want parallel routes. To get the expected behavior you have described you could do this:

export const routes = [{
  path: '/',
  component: Root,
},  {
  path: '/home',
  component: Home,
},
{
  path: '/view',
  component: View,
}];

In this case when it matches to any of these paths it will show the related component.

Nested routes are when you want one route inside the other. In order to do that you need to indicate where it will be inserted. This is done with props.children.

So inside your root component you could:

function Root(props) {
  return <div>Root {props.children}</div>;
}

This will insert the nested route inside this div next to the text Root.

The other thing that is a bit special here is we only match on routes that exist.. We don't render a route without children. So nested routes need to have a pathless or / route inside the children if you want just plain / to work. You can go directly to /home as that would match but we need to have the default route if we want just / to work.. I renamed /home in this case but it could just be a different Component.

Here is the updated example.

https://stackblitz.com/edit/github-aqy729-qdxvdk?file=src%2Fapp.tsx

In any case this isn't a bug so much as a documentation shortcoming.. so moving to docs.

@ryansolid ryansolid changed the title Unable to render nested routes when using route config Clarify why and how nested routing Aug 7, 2024
@ryansolid ryansolid transferred this issue from solidjs/solid Aug 7, 2024
Copy link

stackblitz bot commented Aug 7, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@gxxcastillo
Copy link
Author

gxxcastillo commented Aug 7, 2024

Ah! Of course 🤦. Thanks for the thorough clarification. How do I render an index route with the nesting approach?

For example, if I were to want everything under / to use the same template, including / itself.

@gxxcastillo
Copy link
Author

nm, I got my answer:

export const routes = {
  path: '/',
  component: Template,
  children: [
    {
      path: '/',
      component: Root,
    },
    {
      path: '/home',
      component: Home,
    },
    {
      path: '/view',
      component: View,
    },
  ],
};

@LadyBluenotes LadyBluenotes added improve documentation help wanted Looking for assistance on this issue solid-router Related to Solid Router and removed improve documentation labels Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Looking for assistance on this issue solid-router Related to Solid Router
Projects
None yet
Development

No branches or pull requests

3 participants