0

I have a stackNavigator is displayed in my menu with my configuration

const ProfileStack = createStackNavigator();

function ProfileNavigator() {
  return (
    <ProfileStack.Navigator
      drawerStyle={{ width: '100%', color: 'red' }} // not work
    >
      <ProfileStack.Screen
        name="ProfileScreen"
        component={ProfileScreen}
        options={{
          headerBackTitleVisible: false, // not work
          headerMode: 'none',
          headerShown: true // work
        }}
      />
    </ProfileStack.Navigator>
  )
}

enter image description here ( I have profile in menu ( red border ) and button profile ( icon ) And i want to keep the button, hide in the menu and keep the header in the webpage/view.

I try many conf with the documentation ( https://reactnavigation.org/docs/4.x/stack-navigator-1.0#navigationoptions-for-screens-inside-of-the-navigator )

I would like hide the element of the menu. Because the page/view is accessible from a button. I check many configurations of createStackNavigator. headerBackTitleVisible and others didn't work.

Do you know how to hide an element bevcause i'm lost :( ?

thanks

Seb
  • 404
  • 2
  • 4
  • 14
  • What specifically is showing that you are you wanting to hide? The back button title? The header title? Or something else? A screenshot of what you are seeing might help illustrate the issue. – MoreFoam Dec 06 '20 at 20:59
  • Because I have the impression that I have to have an item in the menu to display a header in the page. I will show a capture. – Seb Dec 06 '20 at 21:12
  • Could you just remove the profile screen from your drawer navigator component? Then have the profile screen be a Screen component in one of your stack navigator components. Then, when you want to navigate to it, just call navigation.navigate("profile"). Of course, replace "profile" with whatever your screen is named. This might be what your button is already doing, but I don't have the code. In summary, if you do not need that screen in your drawer navigator, don't put it as a Screen in the drawer navigator component, rather put it in a stack navigator and navigate to it as needed. – MoreFoam Dec 06 '20 at 22:27
  • Yes if i remove the profile screen in the drawer navigator, I can display the page/view of profile. But the header isn't the drawer navigator. I can't acces of the drawer navigator. hum / I begining of this framework so i search ^^ – Seb Dec 06 '20 at 22:31
  • Oh, I see now. So you want to hide "profile" in the drawer menu, but still be able to access the drawer from the profile screen. Is that correct? – MoreFoam Dec 06 '20 at 22:38
  • exactly... sorry for my bad english :( – Seb Dec 06 '20 at 22:41
  • No worries, you can only get better from here! – MoreFoam Dec 06 '20 at 23:31

1 Answers1

1

Take a look at this React Navigation Github issue. The person who posted that wanted to hide an item in the drawer menu, but still be able to access the drawer from that page. The following workaround was provided:

I found the following workaround :

  • Create a component returning null
  • Pass the component to the drawerLabel navigationOptions of the screen you want the item to be hidden

Hope this helps.

Here is an example below, which you can copy, paste, and run if you would like:

App.js:

import React from 'react';

import TestAppContainer from "./TestAppContainer";


export default function App() {
    return (
        <TestAppContainer/>
    );
}

TestAppContainer.js:

import React from "react";
import {NavigationContainer} from "@react-navigation/native";
import {DrawerNavigatorTest} from "./NavigatorTest";

const TestAppContainer = props => {

    return (
        <NavigationContainer>
            <DrawerNavigatorTest />
        </NavigationContainer>
    );
}

export default TestAppContainer;

NavigatorTest.js:

import React from "react";
import {createDrawerNavigator} from "@react-navigation/drawer";

import TestScreen from "./TestScreen";
import TestScreenTwo from "./TestScreenTwo";
import NullComponent from "./NullComponent";


const TestDrawerNavigator = createDrawerNavigator();

export const DrawerNavigatorTest = () => {
    return(
        <TestDrawerNavigator.Navigator drawerContentOptions={{activeBackgroundColor: "transparent"}}>
            <TestDrawerNavigator.Screen name={"Test"} component={TestScreen} />
            <TestDrawerNavigator.Screen name={"Test2"} component={TestScreenTwo} options={{drawerLabel: NullComponent}}/>
        </TestDrawerNavigator.Navigator>
    );
}

NullComponent.js:

import React from "react";

const NullComponent = props => {
    return null;
}

export default NullComponent;

TestScreen.js:

import React from "react";
import {SafeAreaView, View, Text, Button} from "react-native";

const TestScreen = props => {

    const buttonPressHandler = () => {
        props.navigation.navigate("Test2");
    };

    return (
        <SafeAreaView style={{flex: 1,}}>
            <View style={{flex: 1, alignItems: "center", justifyContent: "center"}}>
                <Text>Test Screen</Text>
                <Button title={"Go to next screen."} onPress={buttonPressHandler} />
            </View>
        </SafeAreaView>
    );
};

export default TestScreen;

TestScreenTwo.js:

import React from "react";
import {SafeAreaView, View, Text} from "react-native";

const TestScreenTwo = props => {

    return (
        <SafeAreaView style={{flex: 1,}}>
            <View style={{flex: 1, alignItems: "center", justifyContent: "center"}}>
                <Text>Test Screen Two</Text>
            </View>
        </SafeAreaView>
    );
};

export default TestScreenTwo;

Note that this is not a perfect solution, but a workaround. It could use some more fine tuning. You may also want to look into custom drawer components. I'm not very familiar with custom drawer components, but you may be able to get cleaner functionality with custom drawer items. See the React Navigation Docs.

MoreFoam
  • 624
  • 1
  • 6
  • 25
  • The following StackOverflow question may have a better answer, but I have not tested it: https://stackoverflow.com/questions/60395508/react-navigation-5-hide-drawer-item – MoreFoam Dec 06 '20 at 23:38
  • ir's ok , it's no visible mais clickable ahah. I don't know if we can in this option screen. use a css. Un css to hidden element, or put a height at 0 ? – Seb Dec 07 '20 at 00:22
  • The best solution is the custom menu. so if a custom menu it's work perfectly. thanks you so much – Seb Dec 08 '20 at 21:00