1--- 2title: Hooks API 3description: Learn how to interact with the in-app URL in Expo Router. 4--- 5 6import { FileTree } from '~/ui/components/FileTree'; 7import { RouteUrlGroup, RouteUrl } from '~/ui/components/RouteUrl'; 8import { APIBox } from '~/components/plugins/APIBox'; 9 10<RouteUrlGroup> 11 12In Expo Router, there's always a valid URL that represents the currently focused route. Use hooks to observe changes and interact with the URL. 13 14## Hooks 15 16<APIBox header="useFocusEffect"> 17 18Given a function, the `useFocusEffect` hook will invoke the function whenever the route is "focused". 19 20```jsx 21/* @info */ 22import { useFocusEffect } from 'expo-router'; 23/* @end */ 24 25export default function Route() { 26 useFocusEffect(() => { 27 /* @info Invoked whenever the route is focused */ 28 console.log('Hello') 29 /* @end */ 30 }) 31 32 return </> 33} 34``` 35 36</APIBox> 37 38<APIBox header="useGlobalSearchParams"> 39 40Returns the URL search parameters for the globally selected route. For example, `/acme?foo=bar` -> `{ foo: "bar" }`. 41 42Refer to the [local vs global search params](/router/reference/search-parameters/#local-vs-global-search-parameters) guide for more info. 43 44<RouteUrl>/profile/baconbrix?extra=info</RouteUrl> 45 46```jsx app/profile/[user].tsx 47import { Text } from 'react-native'; 48/* @info */ 49import { useGlobalSearchParams } from 'expo-router'; 50/* @end */ 51 52export default function Route() { 53 /* @info <b>user=baconbrix</b> & <b>extra=info</b> */ 54 const { user, extra } = useGlobalSearchParams(); 55 /* @end */ 56 return <Text>User: {user}</Text>; 57} 58``` 59 60</APIBox> 61 62<APIBox header="useLocalSearchParams"> 63 64Returns the URL search parameters for the contextually selected route. Refer to the [local versus global search params](/router/reference/search-parameters/#local-vs-global-search-parameters) guide for more information. 65 66<FileTree files={['app/_layout.tsx', 'app/[first]/home.tsx', 'app/[second]/shop.tsx']} /> 67 68When `/abc/home` pushes `/123/shop`, `useGlobalSearchParams` returns `{ first: undefined, second: '123' }` on **/app/[first]/home.tsx** because the global URL has changed. However, you may want the params to remain `{ first: 'abc' }` to reflect the context of the screen. In this case, you can use `useLocalSearchParams` to ensure the params `{ first: 'abc' }` are still returned in **/app/[first]/home.tsx**. 69 70<RouteUrl>/profile/baconbrix?extra=info</RouteUrl> 71 72```jsx app/profile/[user].tsx 73import { Text } from 'react-native'; 74/* @info */ 75import { useLocalSearchParams } from 'expo-router'; 76/* @end */ 77 78export default function Route() { 79 /* @info */ 80 const { user, extra } = useLocalSearchParams(); 81 /* @end */ 82 return <Text>User: {user}</Text>; 83} 84``` 85 86</APIBox> 87 88<APIBox header="useNavigation"> 89 90Access the underlying React Navigation [`navigation` prop](https://reactnavigation.org/docs/navigation-prop) to imperatively access layout-specific functionality like `navigation.openDrawer()` in a Drawer layout. [Learn more](https://reactnavigation.org/docs/navigation-prop/#navigator-dependent-functions). 91 92```jsx 93/* @info */ 94import { useNavigation } from 'expo-router'; 95/* @end */ 96 97export default function Route() { 98 /* @info Access the current navigation object for the current route */ 99 const navigation = useNavigation(); 100 /* @end */ 101 return ( 102 <View> 103 <Text 104 onPress={() => { 105 /* @info Open the drawer view */ 106 navigation.openDrawer(); 107 /* @end */ 108 }}> 109 Open Drawer 110 </Text> 111 </View> 112 ); 113} 114``` 115 116</APIBox> 117 118<APIBox header="usePathname"> 119 120Returns the currently selected route location without search parameters. For example, `/acme?foo=bar` -> `/acme`. Segments will be normalized: `/[id]?id=normal` -> `/normal` 121 122<RouteUrl>/profile/baconbrix?extra=info</RouteUrl> 123 124```jsx app/profile/[user].tsx 125import { Text } from 'react-native'; 126/* @info */ 127import { usePathname } from 'expo-router'; 128/* @end */ 129 130export default function Route() { 131 /* @info <b>pathname = "/profile/baconbrix"</b> */ 132 const pathname = usePathname(); 133 /* @end */ 134 135 return <Text>User: {user}</Text>; 136} 137``` 138 139</APIBox> 140 141<APIBox header="useSegments"> 142 143Returns a list of segments for the currently selected route. Segments are not normalized so that they will be the same as the file path. For example, `/[id]?id=normal` -> `["[id]"]`. 144 145```jsx app/profile/[user].tsx 146import { Text } from 'react-native'; 147/* @info */ 148import { useSegments } from 'expo-router'; 149/* @end */ 150 151export default function Route() { 152 /* @info <b>segments = ["profile", "[user]"]</b> */ 153 const segments = useSegments(); 154 /* @end */ 155 return <Text>Hello</Text>; 156} 157``` 158 159This function can be typed using an abstract of string arrays: 160 161```jsx app/profile/[user].tsx 162import { useSegments } from 'expo-router'; 163 164export default function Route() { 165 /* @info */ 166 const segments = useSegments<['profile'] | ['profile', '[user]']>(); 167 /* @end */ 168 169 return </> 170} 171``` 172 173</APIBox> 174 175## Types 176 177<APIBox header="Href"> 178 179The `Href` type is a union of the following types: 180 181- **string**: A full path like `/profile/settings` or a relative path like `../settings`. 182- **object**: An object with a `pathname` and optional `params` object. The `pathname` can be a full path like `/profile/settings` or a relative path like `../settings`. The `params` can be an object of key/value pairs. 183 184</APIBox> 185 186</RouteUrlGroup> 187