question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Very nested routes quirks

See original GitHub issue

I’m trying to use new 3 version.

Reproduction project: https://codesandbox.io/s/svelte-spa-router-very-nested-routes-mskmc?file=/todo/Index.svelte

Questions:

  1. Can we use one regex for this code in todos/Index.svelte?

    .set("/:ID", Show)
    .set("/:ID/*", Show);
    
  2. Why do we need to use const prefix = "/todos" both in todos/Index.svelte and todos/Show.svelte? Can we just use const prefix = "/:ID" in todos/Show.svelte? (https://github.com/ItalyPaleAle/svelte-spa-router/issues/134)

  3. Can we avoid prefixes at all?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ItalyPaleAlecommented, Oct 7, 2020

First question

There was a small mistake in the regular expression I gave you. It doesn’t work unless you add $ at the end.

In todos/Index.svelte, your match should be (note the addition of the $ at the end of the matching sequence)

.set(/^\/(.*?)(\/(.*?))?$/, Show)

Because the match is using a regexp now, as per the docs I linked above, you can’t use params.ID anymore as you’re doing in the file. Instead, you need to use params[1] (because the ID is in the first matched group).

So, in todos/Show.svelte you also need to change:

<a href={`#/todos/${params.ID}/edit`}>Edit todo</a> | <a href={`#/todos/${params.ID}/notes`}>Todo notes</a>

to:

<a href={`#/todos/${params[1]}/edit`}>Edit todo</a> | <a href={`#/todos/${params[1]}/notes`}>Todo notes</a>

Second question

As I explained above, the prefix has to be relative to the full path being matched. If you’re using a double-nested router, you still need to set /todos in the prefix.

However, in Show.svelte you can set a longer prefix that includes the ID, but:

  1. You’ll have to use a regular expression for the prefix, so something like prefix = /^\/todos(\/(.*))?/i
  2. You’ll need to remove /:ID from the route definition object in Show.svelte, so the inner routes will NOT have access to the ID prop
0reactions
frederikhorscommented, Oct 7, 2020

You’ve made it really clear now.

Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Guide to Nested Routes with React Router - ui.dev
In this comprehensive, up-to-date guide, you'll learn everything you need to know about creating nested routes with React Router.
Read more >
Adjusting rspec routing tests for nested routes - Stack Overflow
First of all, run rake routes to see what routes exist. According to what you have in your routes I would expect you...
Read more >
React App Routing with Wouter — Active Links, Trailing Slash ...
We can add nested paths by setting the base path. For instance, we can write: import React from "react"; import { Link, Route,...
Read more >
React Router Frequently Faced Problems - Kiprosh Blogs
Routing is one of the toughest thing to Implement in Single Page App In ... one quirk in it, Declared component if had...
Read more >
React Router 6: Nested Routes - Robin Wieruch
Nested Routes are a powerful feature. While most people think React Router only routes a user from page to page, it also allows...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found