react native - How to Remove Bar at the top of the screen - Stack Overflow

admin2025-04-29  2

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?

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?

Share Improve this question edited Jan 7 at 5:45 Ken White 126k15 gold badges236 silver badges466 bronze badges asked Jan 6 at 23:44 agent_beanagent_bean 1,5953 gold badges20 silver badges37 bronze badges 1
  • <Stack.Screen name="Menu" component={MenuScreen} options={{ headerShown: false }} /> – Harrison Commented Jan 7 at 3:19
Add a comment  | 

1 Answer 1

Reset to default 1

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 :)

转载请注明原文地址:http://anycun.com/QandA/1745938854a91391.html