Marco Martin
Marco Martin

Reputation: 185

Hide and show navigation header on react native navigation

I'll like to hide the navigation header when clicked on the icon, the code bellow hides the header but do not restore it when search back button is pressed ....

Here is the code:

static navigationOptions = ({ navigation }) => {
    const {params} = navigation.state;

    return {
            title: 'Chats',
            titleStyle: {
                color: '#ffffff',  
            },
            tintColor: '#ffffff',
            headerTintColor: '#ffffff',
            headerStyle: {
                backgroundColor: "#E6A919"
            },
            header: navigation.state.params ? navigation.state.params.header : undefined, 
            headerTitleStyle: {
                color: '#ffffff'
            }, 
            headerRight: () =>     
                                  <TouchableOpacity
                                    onPress={
                                        () => {
                                            
                                            navigation.setParams({ header: null });
                                            navigation.setParams({ showSearch: true });
                                        }   
                                    }                             
                                  >
                                    <Icon
                                        style={{ width: 24, height: 24, margin: 15, color: '#ffffff', backgroundColor: 'transparent'}}
                                        name={"search"}
                                    />
                                  </TouchableOpacity>
    }        
}

onBackPressed = () => {
    console.log("Back pressed!");
    
    this.props.navigation.setParams({ showSearch: false });
    this.props.navigation.setParams({ header: true });
    this.resetSearchText();
};  

What can I do?

Thanks

Upvotes: 0

Views: 184

Answers (2)

Marco Martin
Marco Martin

Reputation: 185

To make it visible again I solved setting 'undefined' to header...

Upvotes: 0

This post answers your question. You're doing right, you just need to add the conditional expression now to return the header only when the navigation.state.params.header is different of undefined.

Upvotes: 1

Related Questions