Child routers: render another location with the parent's history

Aperta
#8,820 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
react, typescript
Ambito
frontend

Direzione di ricerca

Read the prototype linked in the issue, then inspect router.ts and the Transitioner guards it describes. The proposal also calls for tests and a stack-pages example; compare those with the prototype to define the API and behavior. Done means child routers share parent history and blockers, keep navigation relative to the child location, and avoid URL changes and extra global listeners.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

We keep pages mounted under sliding panels: any page can open on top of the current one with stackNavigate('/foo/p1'), and the page below keeps its exact state (form input, filters, scroll). The URL is always the top page, the pages below live in history state.

Right now the only way to do this with TanStack Router is a createRouter per layer plus a hand-written RouterHistory that's fixed to one location. It works, but every router adds scroll restoration listeners that are never removed (#8775), every Transitioner canonicalizes and replaces the URL on mount, self.__TSR_ROUTER__ gets overwritten, and the custom history has to implement _getBlockers/notify.

Proposal, a parent option:

const child = createRouter({ ...router.options, parent: router, location: '/foo/p1' })
child.setLocation('/foo/p2')

A child uses the parent's history and blockers, hands its navigations to the parent (relative to still resolves against the child's location), never reads or writes the URL, adds no global listeners and doesn't touch __TSR_ROUTER__. Hooks stay the same, each child has its own RouterProvider. Out of scope for now: sharing committed match data, SSR, transitions.

I made a prototype to see how it looks: compare. It's ~45 lines in router.ts plus a 2-line guard in each Transitioner, with tests and a stack-pages example. In our app it removes our whole custom layer history.

Is parent an OK shape, or would you prefer something like a free createChildRouter(router, { location })? Happy to turn it into a PR once the API is agreed.

Lingua principale
TypeScript
Stelle
15.3k
Fork
1.9k
Merge medio
1g 5h
PR unite (30g)
46

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/router

Tutte le issue di TanStack/router

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.