react-native

How to Remove Bar at the top of the screen


I have started learning react native.

I have a simple app and started adding navigation.

I'm trying to create a game, and I want to navigate between screens. The code works, but it application has a bar at the top.

App.js

import React from 'react';
import { NavigationContainer } from '@react-navigation/native'; // Only here
import { createStackNavigator } from '@react-navigation/stack';
import MenuScreen from './MenuScreen';
import GameScreen from './GameScreen';
import SettingsScreen from './SettingsScreen';

const Stack = createStackNavigator();

const App = () => {
  return (
      <Stack.Navigator initialRouteName="Menu">
        <Stack.Screen name="Menu" component={MenuScreen} />
        <Stack.Screen name="GameScreen" component={GameScreen} />
        <Stack.Screen name="SettingsScreen" component={SettingsScreen} />
      </Stack.Navigator>
  );
};

export default App;

MenuScreen

import React from 'react';
import { View, Text, Button, StyleSheet } from 'react-native';

const MenuScreen = ({ navigation }) => {
  return (
    <View style={styles.container}>
      <Text style={styles.title}>Game Menu</Text>
      <Button title="Play" onPress={() => navigation.navigate('GameScreen')} />
      <Button title="Settings" onPress={() => navigation.navigate('SettingsScreen')} />
      <Button title="Exit" onPress={() => console.log('Exit Game')} />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#282c34',
  },
  title: {
    fontSize: 30,
    color: '#ffffff',
    marginBottom: 20,
    fontWeight: 'bold',
  },
});

export default MenuScreen;

How do I remove it?

If it is not possible, What should I use as an alternative?

enter image description here


Solution

  • In your app file, within the Stack.Navigator, you can set headerShown: false in the screenOptions.

    let me show it to you in your code.

    const App = () => {
      return (
          <Stack.Navigator 
          initialRouteName="Menu" 
          screenOptions={{
          headerShown: false,
          }}>
            <Stack.Screen name="Menu" component={MenuScreen} />
            <Stack.Screen name="GameScreen" component={GameScreen} />
            <Stack.Screen name="SettingsScreen" component={SettingsScreen} />
          </Stack.Navigator>
      );
    };
    

    Hope this helps to you :)