简体   繁体   English

如何在底部选项卡导航器 react-navigation 中卸载非活动屏幕?

[英]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,我想自动卸载它们。

  • BottomTabNavigator底部标签导航器
    • Stack1堆栈1
      • Screen屏幕
      • Screen屏幕
    • Stack2堆栈2
      • Screen屏幕
      • Screen屏幕

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.我找到了两种卸载方式。

  1. 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

  2. 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.

相关问题 在底部选项卡导航器中卸载非活动屏幕(重置堆栈) - Unmount inactive screens (reset stacks) in bottom tab navigator 如何使底部标签栏仅出现在 react-navigation 中嵌套堆栈的默认屏幕上? - How to make bottom tab bar appear only on default screens of nested stacks in react-navigation? 如何在React-Navigation中单击Bottom Tab Navigator打开DrawerNavigator? - How do i open DrawerNavigator upon clicking Bottom Tab Navigator in React-Navigation? 如何动态更新react-navigation标签导航器的activeTintColor? - how to update activeTintColor for react-navigation tab-navigator dynamically? 如何在反应导航 5 中的选项卡导航器中的选项卡屏幕之间移动? - How to move between tab screens in tab navigator in react navigation 5? 如何在导航到屏幕之前使用密码身份验证并使用 React Native 中的底部选项卡导航器对其进行调整? - How to use password authentication before navigate to screens and adapt it with the bottom tab navigator in React Native? 反应导航-底部导航 - React-navigation - Bottom Navigation 使用 React-Navigation 底部选项卡的交叉淡入淡出过渡 - Cross-Fade transitions using React-Navigation bottom Tab 如何在反应导航中有条件地隐藏 Tab? - How to hide Tab conditionally in react-navigation? 如何使用“@react-navigation/bottom-tabs”在 Tab.Screen 左侧添加 header - How to add header left on Tab.Screen using "@react-navigation/bottom-tabs"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM