From b475c03c8b92422654a5de5d7c36ff4026f0dd17 Mon Sep 17 00:00:00 2001 From: Dmitriy Shishkin <6ast3r@gmail.com> Date: Thu, 3 Dec 2026 11:55:00 +0700 Subject: [PATCH] Document usePreventFocusRestore in stack navigator --- versioned_docs/version-8.x/stack-navigator.md | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/versioned_docs/version-8.x/stack-navigator.md b/versioned_docs/version-8.x/stack-navigator.md index 58b234f027..f37434ba62 100755 --- a/versioned_docs/version-8.x/stack-navigator.md +++ b/versioned_docs/version-8.x/stack-navigator.md @@ -268,6 +268,8 @@ Interpolated styles for various parts of the header. Refer the [Animations secti If `false`, the keyboard will NOT automatically dismiss when navigating to a new screen from this screen. Defaults to `true`. +When enabled, the text input that was focused before navigating away from this screen is focused again when the screen comes back into focus. To skip this for a specific navigation, use the [`usePreventFocusRestore`](#usepreventfocusrestore) hook. + ### Header related options You can find the list of header related options [here](elements.md#header). These [options](screen-options.md) can be specified under `screenOptions` prop of `Stack.Navigator` or `options` prop of `Stack.Screen`. You don't have to be using `@react-navigation/elements` directly to use these options, they are just documented in that page. @@ -660,6 +662,34 @@ See [Transparent modals](#transparent-modals) for an example of how to use this This hook returns the pan gesture for the current Stack screen, or `null` if gestures are not supported on the platform. It can be used to coordinate a custom gesture with the Stack's gesture using the [Pan gesture APIs from `react-native-gesture-handler`](https://docs.swmansion.com/react-native-gesture-handler/docs/gestures/use-pan-gesture/). +#### `usePreventFocusRestore` + +When you go back to a screen, the stack focuses the text input that was focused before you left it. This hook returns a function that skips this once, for the next time the screen comes back into focus. + +It's useful when the screen decides what to focus after another screen returns a result. For example, after picking a country on another screen, you may want to focus the next field or keep the keyboard hidden: + +```js +import { usePreventFocusRestore } from '@react-navigation/stack'; + +function AddressScreen({ route }) { + const preventFocusRestore = usePreventFocusRestore(); + const cityInputRef = React.useRef(null); + + React.useEffect(() => { + if (route.params?.country) { + preventFocusRestore(); + cityInputRef.current?.focus(); + } + }, [route.params?.country, preventFocusRestore]); + + // ... +} +``` + +Call it before the screen is focused again, e.g. in an effect when receiving the params, or in a callback called from the other screen. Outside of a Stack screen, the returned function does nothing. + +To disable the keyboard handling entirely for a screen, use the [`keyboardHandlingEnabled`](#keyboardhandlingenabled) option. + ## Animations You can specify the `animation` option to customize the transition animation for screens being pushed or popped.