How-to
Website
Web app
Keep filters in the address
A filter held in a state is lost on reload and missing from a link the reader shares. To make it survive a reload, a shared link and the back button, read it from query instead:
const PRODUCTS = [
{ name: "Kettle", price: 39 },
{ name: "Apron", price: 18 },
{ name: "Mug", price: 9 },
]
page Products(path: "/products", title: "Products", description: "Everything we sell.") {
derived sort = query.sort ?? "name"
derived shown = if sort == "price" {
PRODUCTS.sortBy(p => p.price)
} else {
PRODUCTS.sortBy(p => p.name)
}
Heading("Products").h1
Row(gap: .sm) {
Link("By name", to: "/products?sort=name")
Link("By price", to: "/products?sort=price")
}
for p in shown by p.name {
Text("{p.name}: ${p.price}")
}
}
How it works
Choose By price and the list reorders to Mug, Apron, Kettle, and the address becomes /products?sort=price. Reload the page or share the link, and it opens sorted by price.
queryis the address's query string as a map:query.sortis"price"for?sort=price, and null when the address has nosort.sortis aderivedvalue, so??gives it a default and it follows the address.shownis derived fromsort, so the list reorders whenever the address does.- Each choice is a
Linkto the same page with another query. A query string is not part of the route, so/products?sort=priceis still theProductspage; onlysortchanges.
To change the query from an action instead of a link, call navigate("/products?sort=price").