Search

The search and searchParams are available in route components as attributes of the location URL.

function Route() {
	let location = useLocation()

	let search = location.search
	let searchParams = location.searchParams
		...
}

SearchParams is an instance of URLSearchParams, so take a look at this native web platform api if you are not familiar.

Loaders with search parameters

Routes that load data based on search params need to specify the search parameters they depend on. To specify search parameters for route components, the same search string syntax as for urls is used in the routes configuration:

let Router = Routes(
	<App path="app">
		<Todos path="todos?filter" loader={todosLoader} />
	</App>,
);

function todosLoader({ url }) {
	let filter = url.searchParams.get('filter');
		...
}

Or if the route needs multiple search parameters:

let Router = Routes(
	<App>
		<Todos path="todos?order&filter" loader={todosLoader} />
	</App>,
);

If you really need access to all search parameters in the loader, do not specify any specific search parameters at all.

let Router = Routes(
	<App>
		<Todos path="todos?" loader={todosLoader} />
	</App>,
);

Matching with search parameters

Search parameters can also take part in the matching strategy of the routes.

Specifying search parameters with assigned values will only match the corresponding route if the url also has the same search parameter with that value.

let Router = Routes(
	<App path="app">
		<Todos path="todos?filter" />
		<Today path="todos?filter=today" />
		<Tomorrow path="todos?filter=tomorrow" />
	</App>,
);

If the url is /app/todos or /app/todos?filter=something the router will render the Todos route. If the url is /app/todos?filter=today the router will render the Today route, and a url /app/todos?filter=tomorrow will render the Tomorrow route component.

The search parameter value can also be omitted if you want to allow any value, matching only when the search parameter is present, whatever its value.

let Router = Routes(
	<App path="app">
		<Todos path="todos" />
		<FilteredTodos path="todos?filter=" />
	</App>,
);

When the filter search parameter is present, it will render the FilteredTodos route. If not, the Todos route wil be rendered.