fenok / react-router-typesafe-routes

Comprehensive and extensible type-safe routes for React Router v6 with first-class support for nested routes and param validation.
MIT License
145 stars 3 forks source link

Route composition API #13

Closed fenok closed 1 year ago

fenok commented 2 years ago

There are cases when routes can share the same set of parameters (most likely search parameters and state fields). One common example is pagination params.

We likely want a common helper for these parameters as well, so we need an actual route for them.

We could use inheritance, but it can quickly get ugly: PAGINATION.FILTER.ORDER.ACTUAL_ROUTE.buildUrl(...).

We can use route types, but it's relatively verbose:

const PAGINATION_FRAGMENT = route( '', { searchParams: { offset: numberType } } );

const LIST = route( 'list', { searchParams: { ...PAGINATION_FRAGMENT.types.searchParams, customParam: stringType } } )

It would be nice to have an API for route composition, something like this:

const LIST = route( 'list', {searchParams: { customParam: stringType }}, {}, [PAGINATION_FRAGMENT] );

However, I can't figure out how to write types for this (when there are multiple fragments).

fenok commented 1 year ago

We can simply compose the types param like this: compose({ searchParams: {query: stringType}})(PAGINATION_FRAGMENT).