5

I am using react-navigation for routing purpose. I want to dynamically hide or show header on one component. Any way to do it?

I change headerLeft dynamically like this but can not find any way to do it for entire header.

static navigationOptions = ({ navigation }) => ({
    headerRight: navigation.state.params ? navigation.state.params.headerRight : null
});

this.props.navigation.setParams({
        headerRight: (
            <View>
                <TouchableOpacity onPress={() => blaa} >
                     <Text>Start</Text>
                </TouchableOpacity>
            </View>
        )
});

I want something like this - hide/show header based on state:

this.props.navigation.setParams({
        header: this.state.header
});

4 Answers 4

17

Got it working:

Don't know why it is so but passing undefined to header will show default header and null will hide the header.

I am doing something like this:

static navigationOptions = ({ navigation }) => ({
    header: navigation.state.params ? navigation.state.params.header : undefined
});

and on state change;

this.props.navigation.setParams({ 
        header: null 
});
Sign up to request clarification or add additional context in comments.

3 Comments

@kaya null hides the header. that is fine. what I dint get is how header is resetting to original on setting it undefined dynamically.
I didn't find it in docs but I can say that undefined means not defined so it resets the value.
@kaya Yes that is possible.
1

Instead of using setParams as stated by other answers, you can use setOptions directly. Something like:

navigation.setOptions({ headerShown: false })

You can take navigation from the route props or using the useNavigation hook

This way you don't need to add logic to your route, but apply the effect directly

Comments

0

According to the docs, setting the header to null hides the header. Go about it like this

this.props.navigation.setParams({
  header: this.state.header ? whatever-you-want : null
})

3 Comments

Hiding it is easy with null but what should be the value for 'whatever-you-want'
you said you wanted to dynamically hide or show the header component right? whatever-you-want should be substituted for a react element or function. reactnavigation.org/docs/navigators/stack#header that you want to be shown as the header
here header is not separate react element, header is a part of react-navigation library.
0

I need to set both of headerShow and header options:

on fullscreen

navigation.setOptions({ headerShown: false, header: null });

on exist fullscreen

navigation.setOptions({ headerShown: true, header: undefined });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.