How to Add a Back Button to your React Navbar
Tags: webdev, beginners, react, superflows
Published: 2022-11-16 05:23:42

## Introduction

This article shows you how to add a back button to your react navbar. It shows you how to show / hide the back button, how to change its icon, how to handle the callback and then how to do further styling.

## Prerequisites

This article assumes that you have read the previous navbar tutorials. In case you haven't you will find a consolidated list in the reference links section. This article assumes that you have installed the Superflows library, have the default navigation bar up and running, have added your brand information, have also customised your menu, have added the search input, have configured the sign in button and have also understood how to insert user profile information into the navbar. This tutorial takes it forward from there.

## Step 1 - Display the Back Button

By default, the menu button is shown and the back button is not shown. To show the back button, set the showBack prop to true. Please note that after the back button is displayed, the menu button is not shown. Only one of these two buttons can be shown at one time. Usually the back button on the navbar will be required on the inner screens, where the menu, search and sign in button may also not be required. You can remove them as well.

```
function Apps(props) {
  
    return (
        <div>
            <div>
                <SfNav 
                  showBack={true} 
                  menu={[]} 
                  showSearch={false} 
                  showSignIn={false} 
                />
            </div>
        </div>
    );
}
```
It renders as follows:

![Back](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bouqlj2ymxng4kvqaccg.png)

![Back Mobile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/omwb255z5gcmct77p0ju.png)

## Step 2 - Change the Icon

You can change the back icon by passing your own icon object to the backIcon prop. This example uses an icon from the React Bootstrap icon library. You can use any other icon library as well.

```
import {ArrowLeft} from 'react-bootstrap-icons'
```

```
function Apps(props) {
  
    return (
        <div>
            <div>
                <SfNav 
                  showBack={true} 
                  menu={[]} 
                  showSearch={false} 
                  showSignIn={false} 
                  backIcon={<ArrowLeft style={{paddingTop: '5px'}} />}
                />
            </div>
        </div>
    );
}
```

It renders as follows:

![Back icon](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhgrrc4jws2tnxtqlpn6.png)

![Back icon mobile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2k2z84uiy2jhjg4bmvga.png)
## Step 3 - Handle the Button Press Callback

You can receive a callback after user presses the back button, after subscribing to the onBackPressed prop.

```
function Apps(props) {
  
    return (
        <div>
            <div>
                <SfNav 
                  showBack={true} 
                  onBackPressed={() => {alert('back pressed');}} 
                  menu={[]} '    
                  showSearch={false} 
                  showSignIn={false} 
                />
            </div>
        </div>
    );
}
```

## Step 4 - Styling

If you are interested in obtaining complete control over the look and feel, you already have it. You can completely override the base css properties of the back icon. An example is show below:

```
function Apps(props) {
  
    return (
        <div>
            <div>
                <SfNav 
                  showBack={true} 
                  onBackPressed={() => {alert('back pressed');}} 
                  menu={[]} 
                  showSearch={false} 
                  showSignIn={false} 
                  stylesBack={{backgroundColor: 'black', color: 'white', padding: '0px', width: '20px', height: '20px', textAlign: 'center', lineHeight: '1.1', borderRadius: '10px', marginRight: '10px'}} 
                  />
            </div>
        </div>
    );
}
```

It renders as follows:

![Back style](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9b5gtymh7wgtnygo6bx.png)

![Back style mobile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zyg102mdifbebcmqokd2.png)

## Reference Links

### Documentation

[This link](https://superflows.dev/docs/building-blocks/nav)

### Example Project For This Tutorial

[This link](https://stackblitz.com/edit/react-ts-bijwwa?file=App.tsx)

### Previous Tutorials of Navbar

- [T1: Getting Started With Navbar](https://blog.superflows.dev/how-to-get-started-with-a-navigation-bar-in-your-react-project-in-2-mins)
- [T2: Insert Brand Info](https://blog.superflows.dev/how-to-customize-the-react-navbar-with-your-brand-info)
- [T3: Customise Menu](https://blog.superflows.dev/how-to-customize-the-menu-of-your-react-navbar)
- [T4: Add Search Functionality](https://blog.superflows.dev/how-to-implement-search-functionality-in-your-react-navbar)
- [T5: Add Sign In Button](https://dev.to/superflowsdev/how-to-add-a-sign-in-button-to-your-react-navbar-1c5b)
- [T6: Add User Profile Info](https://dev.to/superflowsdev/how-to-insert-user-profile-info-into-your-react-navbar-4hn2)

### YouTube Channel

[This link](https://www.youtube.com/channel/UCYNJLCE48yir4DsquciBuDw)

### Discord Community

[This link](https://discord.gg/ksEXS4P9h6)

### Example Projects Collection

[This link](https://stackblitz.com/@superflows-dev/collections/navigation-bar)

## Video Tutorial

{% youtube r8VIwvYWOCU %}

## Further Reading

You have already seen how to get started with the navigation bar, how to insert brand information into it, how to customise the menu and now, how to configure the search input, how to use the sign in button, how to insert profile information into it and now, how to add a back button.

## Conclusion

This article shows you how to add back button into your react navigation bar, how to change the back icon, how to handle its callback and how to style it.