Author avatar

Kimaru Thagana

Customizing Tabs in React Bootstrap

Kimaru Thagana

  • Sep 17, 2020
  • 6 Min read
  • 107 Views
  • Sep 17, 2020
  • 6 Min read
  • 107 Views
Web Development
Front End Web Development
Client-side Frameworks
React

Introduction

In frontend development, a developer has to build a user interface (UI) with user experience (UX) in mind. With Bootstrap tabs, React developers can do this by splitting content into different panes where each pane is viewable to a user one at a time. This approach can be useful when creating stepper-like components that need a user's approval before navigating to the next tab. It is also a UX technique that presents information in separate panes instead of one large body of text. This makes the website seem more organized.

Consider a scenario in which you, as a React developer, are designing your own portfolio website. You want to display your information in the most organized manner and you choose three tabs. The first displays work history (profile page), the second displays education and academic history (home page) and the third displays your contact information (contact page).

In this guide, you will learn how to customize tabs with React Bootstrap using React.js. The guide assumes that you have basic knowledge of React.js.

Set Up a Sample App

Open your terminal and run these commands to create a basic React app.

1
2
3
4
5
npx create-react-app react-bootstrap-tabs

cd react-bootstrap-tabs

npm start
bash

Include React Bootstrap in your basic React app.

1
npm install react-bootstrap bootstrap
bash

Delete the code in the newly created App.js file and paste the sample code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import React, {Component} from 'react';

class App extends Component{
    constructor(props) {
        super(props);
        
    }

    render() {
        return (
            <div>
                
            </div>
        );
    }

}


export default App;
js

In your app.js file, include the stylesheet as well.

1
import 'bootstrap/dist/css/bootstrap.min.css';
js

You can now import Bootstrap components, for example:

1
import { Button } from 'react-bootstrap';
js

Set Up Tabs

In the sample code below, you will create three separate tabs with different content in each pane. Each pane will be a React component. You will then add the tabs component from React Bootstrap. Remember to import the components if you get errors in your code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 import React, {Component} from 'react';
 import 'bootstrap/dist/css/bootstrap.min.css';
 import {Tabs, Tab, Modal, Row, Button, Col, Form, Card, Container} from "react-bootstrap";
 
 class App extends Component{
     constructor(props) {
         super(props);
       
     }
 
     render() {
         return (
             <Container>
                 <Row>
                     <Col>
                         <Tabs defaultActiveKey="Home" 
                               id="controlled-tab-example">
                             <Tab eventKey="home" title="Home">
                                 <Home />
                             </Tab>
                             <Tab eventKey="profile" title="Profile">
                                 <Profile />
                             </Tab>
                             <Tab eventKey="contact" title="Contact" disabled>
                                 <Contact />
                             </Tab>
                         </Tabs>
                     </Col>
                 </Row>
             </Container>
         );
     }
 }
 
 export default App;
js

In the code above you have a tabs component, which has a set defaultActiveKey set to load your Home component first when the page loads. Inside the Tabs component, there are tabs that load different panes. The eventkey checks which tab to load.

Now create three separate React components that will be added into the main App.js file. Create three files in your root folder named Home.js, Profile.js, and Contact.js. Copy the sample code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import React, {Component} from "react";

import 'bootstrap/dist/css/bootstrap.min.css';


class Home extends  Component{
    constructor(props) {
        super(props);
        
    }

    render() {
        return (
            <div>
                This is my Home Page
            </div>
        );
    }

}
export default Home;
js

Profile Component.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import React, {Component} from "react";

import 'bootstrap/dist/css/bootstrap.min.css';


class Profile extends  Component{
    constructor(props) {
        super(props);

    }

    render() {
        return (
            <div>
                This is my Profile Page
            </div>
        );
    }

}
export default Profile;
js

Contact Component.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import React, {Component} from "react";

import 'bootstrap/dist/css/bootstrap.min.css';


class Contact extends  Component{
    constructor(props) {
        super(props);

    }

    render() {
        return (
            <div>
                This is my Contact Page
            </div>
        );
    }

}
export default Contact;
js

You might experience errors in your App.js file. Your components are initialized, but your App.js file has no idea where your components are. Add these lines to your App.js to import your components.

1
2
3
import Home from "./Home";
import Profile from "./Profile";
import Contact from "./Contact";
js

Now you have a fully functional React app with tabs. Each tab, when clicked, will present a different pane with different information.

Conclusion

In this guide, you have developed custom tabs using React.js and React Bootstrap. This knowledge is vital to developers in frontend and React developer positions. Using tabs in websites consolidates information, thus allowing for better UX.

To further build on this guide, learn more about React Bootstrap components from the official site.

0