A product screen needs the sku its dynamic segment captured, and often a few options from the query string too, such as the selected color or a sort order. Expo Router hands a screen both kinds through one hook, offers a second hook for code that follows the URL wherever it goes, and lets you change the query string without opening a new screen.
Key terms
- Search parameters: serializable fields appended to the URL, such as
colorin/products/blue-mug?color=navy, also called query params. - useLocalSearchParams: the hook that returns the URL params of the route the component belongs to.
- useGlobalSearchParams: the hook that returns the params of the route currently selected in the app, whichever screen calls it.
- setParams: the router method that updates the current route's query params.
Route and search params in one object
Route parameters are what match a route, and search parameters carry data between pages. On a dynamic route, useLocalSearchParams returns both in one object. With src/app/products/[sku].tsx, the URL /products/blue-mug?color=navy&qty=2 gives this:
tsx// { sku: 'blue-mug', color: 'navy', qty: '2' }
Every value is a string, an array of strings or undefined, never a number. qty arrives as '2' even when the link passed the number 2, because resolving the href turned it into text with toString(). A check like qty === 2 is therefore always false, so convert with Number(qty) before doing arithmetic.
A search key that appears once gives one string, and a repeated key such as ?tag=sale&tag=new gives an array. On a catch-all route, search params still come back as single strings. Values arrive URL-decoded.
A matched route parameter is never nullish. A search parameter missing from the URL comes back as undefined, so give optional ones a default where you read them:
tsxconst { sort = 'newest' } = useLocalSearchParams<{ sort?: string }>();
Layout Screen components take no initialParams prop, so a default where you read the param is how a screen gets one. The generic sets the type of the returned object. Declare values as string or string[], since that is what the URL delivers.
Local and global params in a stack
A stack keeps the previous screen mounted when it pushes a new one, so several screens can read params at the same moment. Both hooks come from expo-router, are called the same way and differ only in how often they update.
useLocalSearchParams returns the params of the screen's own route and only updates when the URL matches that route. useGlobalSearchParams returns the params of the selected route regardless of which screen calls it, and updates on every URL param change.
Say you push /products/oak-desk on top of /products/blue-mug. The blue mug screen, now in the background, still gets sku: 'blue-mug' from useLocalSearchParams, so its data is still there when the user goes back. Had it read useGlobalSearchParams, it would re-render and receive sku: 'oak-desk', the params of the screen on top.
A plain Link or router.navigate to /products/oak-desk also pushes a new screen, because a different value in a dynamic segment counts as a different screen. The same sku with a different query string reuses the focused screen.
In a stack, load a screen's data from useLocalSearchParams, which only updates when its route is focused. Keep useGlobalSearchParams for analytics or other background work that doesn't draw to the screen, since overusing it can cause performance issues.
Changing the query string with setParams
Get the router from useRouter() and call router.setParams with the params to change. The new values are shallow-merged into the current route's params, and nothing new goes onto the history stack: the user stays on the same screen. The ProductScreen below reads sku and an optional color, and lets the user pick a color.
The destructuring default color = 'navy' gives the screen a value before anyone taps. Each tap calls router.setParams({ color: option }), which updates the color query param of the current route, and useLocalSearchParams returns the new value.
A Link to the same URL with a different query string does the same job. Use setParams for search params on the current screen, and link or push to another product's URL when the route parameter itself should change.
Code Examples
import { useLocalSearchParams, useRouter } from 'expo-router';
import { Pressable, Text, View } from 'react-native';
const colors = ['navy', 'sand', 'forest'];
export default function ProductScreen() {
const router = useRouter();
const { sku, color = 'navy' } = useLocalSearchParams<{ sku: string; color?: string }>();
return (
<View>
<Text>Product {sku}</Text>
{colors.map((option) => (
<Pressable key={option} onPress={() => router.setParams({ color: option })}>
<Text>{option === color ? `${option} (selected)` : option}</Text>
</Pressable>
))}
</View>
);
}