在 React Native 中隐藏底部选项卡导航时收到警告

Received warning when Hide Bottom Tab Navigation in React Native

我试图在特定屏幕中隐藏底部导航时收到警告。警告是 'Cannot update a component from inside the function body of a different component',所以我想做的是我有一个主屏幕,当我导航到详细信息屏幕时,底部导航将转到 disappear/hidden。我的代码如下:

ProductNavigation.js my stack navigation

import 'react-native-gesture-handler';
import React, {useState, useEffect} from 'react'
import { StyleSheet, Text, View } from 'react-native'
import { createStackNavigator } from '@react-navigation/stack'
import MainScreen from '../screen/MainScreen'
import DetailScreen from '../screen/DetailScreen'

  const Stack = createStackNavigator();

  const ProductNavigation = ({navigation, route}) => {
    if (route.state) {
    navigation.setOptions({
        tabBarVisible: route.state.index > 0 ? false: true
      })
    }

    return (
      <Stack.Navigator>
        <Stack.Screen name="Home" component={MainScreen} />
        <Stack.Screen name="Detail" component={DetailScreen} options = {({ route }) => ({title: 
          route.params.productName })}/>
      </Stack.Navigator>
        )
      }

       export default ProductNavigation;

BottomTabNav.js my bottom navigation

import React from 'react'
import { StyleSheet, Text, View } from 'react-native'
import Ionicons from 'react-native-vector-icons/Ionicons'
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'
import ProductNavigation from './ProductNavigation'
import SettingScreen from '../screen/SettingScreen'



  const BottomTab = createBottomTabNavigator();

  const BottomTabNav = ({ navigation, route }) => {

   return (
    <BottomTab.Navigator>
        <BottomTab.Screen 
        name="Home" 
        component={ProductNavigation} 
        options={{
            tabBarLabel: "Home",
            tabBarIcon:({color, size}) => (
                <Ionicons name="home-outline" color={color} size={size} />)
            }} />

        <BottomTab.Screen 
        name="Settings" 
        component={SettingScreen}
        options={{
            tabBarLabel: "Settings",
            tabBarIcon: ({color, size}) => (
                <Ionicons name="settings-outline" color={color} size={size} />)
            
        }} />
    </BottomTab.Navigator>
   )
  }

   export default BottomTabNav

是的,它隐藏在详细信息屏幕中,但为什么会有警告?我应该在哪里编辑或更改我的代码?

查看 navigation.setOptions (https://reactnavigation.org/docs/navigation-prop/#setoptions) 的文档,他们将逻辑放在 useLayoutEffect 钩子中。

尝试更改:

import React from 'react'

...

  if (route.state) {
    navigation.setOptions({
      tabBarVisible: route.state.index > 0 ? false: true
    })
  }

import React, { useLayoutEffect } from 'react'

...

...

  useLayoutEffect(() => {
    if (route.state) {
      navigation.setOptions({
        tabBarVisible: route.state.index > 0 ? false: true
      })
    }
  }, [navigation, route])