简体   繁体   English

如何在功能组件 React Native 中删除 Header

[英]How to remove Header in functional component React Native

I'm setting up a navigation in react-native using only functional component.我正在仅使用功能组件在 react-native 中设置导航。 How do i remove the header on the Screen?如何删除屏幕上的标题?

const AppScreen = ({ navigation }) => {

  //Desc => removing header
  AppScreen.navigationOptions = {
    header: null
  };

  return (
    <>
      <Text>LoGinScreen</Text>
    </>
  );
};

No error message is shown but the app renders with a header.未显示错误消息,但应用程序呈现标题。

It is common to want to configure the headers in a similar way on multiple screens.在多个屏幕上以类似的方式配置headers是很常见的。

class AppScreen extends React.Component {
  static navigationOptions = {
    header: null,
    /* No more header config here! */
  };

  /* render function, etc */
}

/* other code... */

You can move the configuration to the stack navigator under Properties defaultNavigationOptions .您可以将配置移至 Properties defaultNavigationOptions下的堆栈导航器


headerMode Specifies how the header should be rendered: headerMode指定应如何呈现标题:

  • float - Render a single header that stays at the top and animates as screens are changed. float - 渲染一个位于顶部的标题,并随着屏幕的变化进行动画处理。 This is a common pattern on iOS.这是 iOS 上的常见模式。
  • screen - Each screen has a header attached to it and the header fades in and out together with the screen. screen - 每个屏幕都有一个标题,标题与屏幕一起淡入淡出。 This is a common pattern on Android.这是 Android 上的常见模式。
  • none - No header will be rendered. none - 不会呈现标题。

const RootStack = createStackNavigator(
  {
    Apps: AppScreen,
    Details: DetailsScreen,
  },
  {
    initialRouteName: 'Apps',
    headerMode: 'none'
    /* if use header The header config from Apps is now here */
  }
);

You can remove header from functional component您可以从功能组件中删除标题

 const AppScreen = ({ navigation }) => {
 return (
   <Text>LoginScreen</Text>
 );
};

by using this out side of your functional component.通过在功能组件之外使用它。

AppScreen.navigationOptions = {
header: null
};

To remove the header on the screen for functional components, in the routes config do the following:要删除功能组件屏幕上的标题,请在路由配置中执行以下操作:

const AuthStack = createStackNavigator(
  {
    Login: AuthScreen
  },
  {
      headerMode: 'none'   //this will remove the header from the screen of AuthScreen
  })

Set the defaultNavigationOptions in your routes configs:在你的路由配置中设置 defaultNavigationOptions:

createStackNavigator({
                screenName:
                    { screen: screenName },                            
                },
                {defaultStackNavigationOptions:{
                    header: () => null   //this in the screen where you want to hide the header
                       }
                    }
                )

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

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