Skip to content

Commit a5d501a

Browse files
committed
Add migration instructions for unmountOnBlur
1 parent 5700111 commit a5d501a

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

versioned_docs/version-7.x/upgrading-from-6.x.md

+40-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ The `popToTopOnBlur` option provides an alternative approach - it pops the scree
336336

337337
See [Bottom Tab Navigator](bottom-tab-navigator.md#poptotoponblur) and [Drawer Navigator](drawer-navigator.md#poptotoponblur) docs for usage.
338338

339-
It's still possible to achieve the old behavior of `unmountOnBlur` by using the useIsFocused hook in the screen:
339+
It's still possible to achieve the old behavior of `unmountOnBlur` by using the [`useIsFocused`](use-is-focused.md) hook in the screen:
340340

341341
```js
342342
const isFocused = useIsFocused();
@@ -346,7 +346,45 @@ if (!isFocused) {
346346
}
347347
```
348348

349-
This could also be combined with the new [layout props](#new-layout-props) to specify it at the screen configuration level.
349+
This could also be combined with the new [layout props](#new-layout-props) to specify it at the screen configuration level to make the migration easier.
350+
351+
To achieve this, define a component that uses the `useIsFocused` hook to conditionally render its children:
352+
353+
```js
354+
function UnmountOnBlur({ children }) {
355+
const isFocused = useIsFocused();
356+
357+
if (!isFocused) {
358+
return null;
359+
}
360+
361+
return children;
362+
}
363+
```
364+
365+
Then use the component in `layout` prop of the screen:
366+
367+
```diff lang=js
368+
<Tab.Screen
369+
name="MyScreen"
370+
component={MyScreen}
371+
+ layout={({ children }) => <UnmountOnBlur>{children}</UnmountOnBlur>}
372+
options={{
373+
- unmountOnBlur: true,
374+
}}
375+
/>
376+
```
377+
378+
Or `screenLayout` prop of the navigator:
379+
380+
```diff lang=js
381+
<Tab.Navigator
382+
+ screenLayout={({ children }) => <UnmountOnBlur>{children}</UnmountOnBlur>}
383+
screenOptions={{
384+
- unmountOnBlur: true,
385+
}}
386+
>
387+
```
350388

351389
#### The `tabBarTestID` option is renamed to `tabBarButtonTestID` in Bottom Tab Navigator and Material Top Tab Navigator
352390

0 commit comments

Comments
 (0)