简体   繁体   English

React Navigation Header 总是显示

[英]React Navigation Header Is always shown

I am trying to create multiple screens and to switch between them I thought react-navigation would be great.我正在尝试创建多个屏幕并在它们之间切换我认为 react-navigation 会很棒。 But I am getting this problem of header appearing automatically, despite setting header to null.但是,尽管将 header 设置为 null,但我还是遇到了 header 自动出现的问题。 I have my App.js createStackNavigation as:我的 App.js createStackNavigation 为:

const AppNavigator = createStackNavigator({


register : {
  screen: RegisterScreen, 
},

login: {
  screen: LoginScreen,
},

home : {
  screen: HomeScreen
},

  },

  {
    navigationOptions: {
      header:null,
      headerMode: 'none',
    }
  }
  );

Here I have three screens.这里我有三个屏幕。 Namely register, login and home.即注册、登录和主页。 Inside register screen I have other createStackNavigator for registerFinal, login and register screen itself.在注册屏幕内,我还有其他用于 registerFinal、登录和注册屏幕的 createStackNavigator。 It is shown below.如下所示。

const registerNavigation = createStackNavigator(
    {
        register: {
            screen: RegisterScreen,
            headerMode: 'none',
            navigationOptions: ({ navigation }) => ({
                header: null,
              }),
        }, 
        registerFinal: {
            screen: RegisterFinalScreen, 
            navigationOptions: ({ navigation }) => ({
                header: null,
                headerShown: false

              }),
        } ,
        login: {
            screen: LoginScreen, 
            navigationOptions: ({ navigation }) => ({
                header: null,
                headerShown: false
              }),
        },
    },

    {
        navigationOptions: ({ navigation }) => ({
            header: null,
            headerShown: false
          }),
    }
    );

Though, I have header disabled in navigationOptions using both methods as specified in other questions, none of them seems to work for me.虽然,我使用其他问题中指定的两种方法在 navigationOptions 中禁用了 header,但它们似乎都不适合我。 I am getting two header in my screen.我的屏幕上有两个 header。 How do I solve this?我该如何解决这个问题?

Restarting the emulator solved the problem.重新启动模拟器解决了这个问题。 OMG.我的天啊。 This thing had me change my project so many times.这件事让我改变了我的项目很多次。 But sometimes, you just have to laugh at it.但有时,你只需要一笑了之。

Another way of hiding Headers are:另一种隐藏标题的方法是:

export default class LoginPage extends Component{
  static navigationOptions = { // remove header on this page
      header: null
  }
}

Use headerMode not headerShown on the registeration stack as well.在注册堆栈上也使用headerMode而不是headerShown

Basically change your registeration stack to reflect the following:基本上更改您的注册堆栈以反映以下内容:

const registerNavigation = createStackNavigator(
    {
        register: {
            screen: RegisterScreen,
            headerMode: 'none',
            navigationOptions: ({ navigation }) => ({
                header: null,
            }),
        },
        registerFinal: {
            screen: RegisterFinalScreen,
            navigationOptions: ({ navigation }) => ({
                header: null,
                headerMode: 'none'

            }),
        },
        login: {
            screen: LoginScreen,
            navigationOptions: ({ navigation }) => ({
                header: null,
                headerMode: 'none'
            }),
        },
    },

    {
        navigationOptions: ({ navigation }) => ({
            header: null,
            headerMode: 'none'
        }),
    }
);const registerNavigation = createStackNavigator(
    {
        register: {
            screen: RegisterScreen,
            headerMode: 'none',
            navigationOptions: ({ navigation }) => ({
                header: null,
            }),
        },
        registerFinal: {
            screen: RegisterFinalScreen,
            navigationOptions: ({ navigation }) => ({
                header: null,
                headerMode: 'none'

            }),
        },
        login: {
            screen: LoginScreen,
            navigationOptions: ({ navigation }) => ({
                header: null,
                headerMode: 'none'
            }),
        },
    },

    {
        navigationOptions: ({ navigation }) => ({
            header: null,
            headerMode: 'none'
        }),
    }
);

Hope this Helps!希望这可以帮助!

headerMode: 'none' should not be inside navigationOptions : headerMode: 'none'不应在navigationOptions内:

const AppNavigator = createStackNavigator({
 // ...
}, {
  // No `navigationOptions` here
  headerMode: 'none'
})

Most of the times you will need to restart EXPO or RN cli in order for Simulator to take the change.大多数情况下,您需要重新启动EXPORN cli以便模拟器进行更改。


This is how you hide the header on one screen这就是你如何在一个屏幕上隐藏 header

`yourComponentName`.navigationOptions = () => {
    return {
        headerShown: false
    }
}

//above code needs to be before you EXPORT your component

export default `yourComponentName`

You could also hide header on all screens, check like this.您也可以在所有屏幕上隐藏header ,像这样检查。

Tip: instead of adding headerShown: false to each screen's option, you can add screenOptions={{ headerShown: false }} to the navigator to set the option for all of the screens.提示:您可以将screenOptions={{ headerShown: false }}添加到导航器以设置所有屏幕的选项,而不是添加headerShown: false到每个屏幕的选项。

https://medium.com/@satya164/tip-instead-of-adding-headershown-false-to-each-screens-option-you-can-padd-screenoptions-f67baf1e1eae][1] https://medium.com/@satya164/tip-instead-of-adding-headershown-false-to-each-screens-option-you-can-padd-screenoptions-f67baf1e1eae][1]

Create one object as below创建一个 object 如下

const screenOptions = {
    headerShown: false,
};

And then use options like I used below然后使用我在下面使用的选项

    <NavigationContainer>
        <Stack.Navigator initialRouteName="Home"  >
            <Stack.Screen name="Home" component={Home} options={screenOptions} />
        </Stack.Navigator>
    </NavigationContainer>

Following this will not show component name when called.之后调用时不会显示组件名称。

Final version will look like this最终版本将如下所示

import React from 'react';
import { createStackNavigator } from '@react-navigation/stack';
import { NavigationContainer } from '@react-navigation/native';
import Home from './screens/Home';

export default function RootNavigation() {
const Stack = createStackNavigator();

const screenOptions = {
    headerShown: false,
};

return (
    <NavigationContainer>
        <Stack.Navigator initialRouteName="Home"  >
            <Stack.Screen name="Home" component={Home} options={screenOptions} />
        </Stack.Navigator>
    </NavigationContainer>
);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM