[英]How to unmount inactive screens in bottom tab navigator react-navigation?
Im using react-navigation for react-native.我将 react-navigation 用于 react-native。 Is there an option to make that inactive tab screens get unmounted like unmountInactiveRoutes: true in DrawerNavigator??
是否有一个选项可以使非活动选项卡屏幕像在 DrawerNavigator 中的 unmountInactiveRoutes: true 一样被卸载? I cant find something like unmountInactiveRoutes for BottomTabNavigator.
我无法为 BottomTabNavigator 找到类似 unmountInactiveRoutes 的内容。
I have two stacknavigators inside a BottomTabNavigator and I want to unmount them automatically.我在 BottomTabNavigator 中有两个 stacknavigator,我想自动卸载它们。
You can unmount screens in bottom tab by adding option in navigation screenOptions:您可以通过在导航屏幕选项中添加选项来卸载底部选项卡中的屏幕:
unmountOnBlur: true
You can do it in Tab & Drawer Navigations but not in Stack Navigation.您可以在选项卡和抽屉导航中执行此操作,但不能在堆栈导航中执行此操作。
And you can also add unmount individual screen by adding same option in Tab or Drawer Screen option.您还可以通过在选项卡或抽屉屏幕选项中添加相同的选项来添加卸载单个屏幕。
So I don't know if you can unmount components that are inactive personally I did not find it however this is my workaround withNavigationFocus(FocusStateLabel)
and if isFocused
is false.所以我不知道您是否可以亲自卸载不活动的组件我没有找到它但是这是我的解决方法
withNavigationFocus(FocusStateLabel)
并且如果isFocused
为false。 returning null.返回 null。 So this will give you more or less what you are looking for.
所以这或多或少会给你你正在寻找的东西。 If isFocused is true, you'll render what you usually render.
如果 isFocused 为真,您将渲染通常渲染的内容。 If false you'll return null.
如果为 false,您将返回 null。 resulting in the unmounting of your components
导致组件卸载
Some reference https://reactnavigation.org/docs/en/with-navigation-focus.html一些参考https://reactnavigation.org/docs/en/with-navigation-focus.html
I tried Ubaid's answer it works.我尝试了 Ubaid 的答案,它有效。 But you can try this one too: Use
但是你也可以试试这个:使用
import {useIsFocused} from '@react-navigation/native';
const isFocused = useIsFocused();
useEffect(() => {
// Do whatever you want to do when screen gets in focus
}, [props, isFocused]);
It works perfectly fine.它工作得很好。
I found two way unmount.我找到了两种卸载方式。
First method is just trigger the unmount using useFocusEffect
.第一种方法是使用
useFocusEffect
触发卸载。 With my experience this is not completely unmount component.以我的经验,这不是完全卸载组件。 It just trigger only unmount function to unsubscribe events.
它只会触发卸载 function 来取消订阅事件。 https://reactnavigation.org/docs/function-after-focusing-screen/#triggering-an-action-with-a-focus-event-listener
https://reactnavigation.org/docs/function-after-focusing-screen/#triggering-an-action-with-a-focus-event-listener
Second method is completely unmount component when the navigating.第二种方法是在导航时完全卸载组件。 This one is working as react unmount.
这个正在作为反应卸载。 https://reactnavigation.org/docs/bottom-tab-navigator/#unmountonblur
https://reactnavigation.org/docs/bottom-tab-navigator/#unmountonblur
<Tab.Navigator screenOptions={{unmountOnBlur: true}}>
</Tab.Navigator>
In your tab screens在您的标签屏幕中
const unMount = ()=>{
//unmount what you want
}
useEffect(()=>{
return unMount;
},[])
Modify your code with this用这个修改你的代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.