postImage

How to create beautiful React Bootstrap tabs

blogImage

By Emmanuel Chinonso

Web Developer

How to Create a beautiful React Bootstrap tab component with Contrast.

React Bootstrap Tabs are components that separate content placed in the same wrapper but in a separate pane. Only one pane can be displayed at a time.

In this article, we’ll be creating a react tab example using a react-bootstrap library known as Contrast. Contrast, also known as CDBReact, is a react library which is an Elegant UI kit with full bootstrap support that has reusable components for building mobile-first, responsive websites and web apps.

React Bootstrap Tabs are compatible with ReactRouter, meaning that <NavLink> components inside can be used for one-page tabs and general routing solutions for the main navigation. Make sure to include the wrapping <BrowserRouter>. If you are using the navbar to provide a navigation bar, be sure to add appropriate role attributes to the <NavLinks> (role="tab").

Prerequisites

The react-bootstrap tab would be built using React, Bootstrap, and CDBReact. You don’t need to have any previous knowledge of CDBReact but the following are necessary:

  • JavaScript Knowledge
  • Basic React Knowledge
  • Basic Bootstrap Knowledge
  • Node and its package manager, npm. You can install it fromhere. Run the command node -v && npm -v to verify you have them installed.
  • Alternatively, we can use another package manager,Yarn.

The react tab example we are going to build requires the pro version and it will look like the image below

React Bootstrap Tabs

Setup

First Check that you have the node installed. To do this, run the following command in your terminal.

r
node -v

This should show you the current version of the node you have installed on your machine. If you don’t have node.js installed, download it here. Installing node also installs npm on your PC, but you can still confirm using npm-v. Now that we have the node installed, we can start up our React project by going to the directory of our choice and running.

r
npx create-react-app tab-app

I named the project tab-app but you can use whatever name of your choice.

Installing CDBReact-pro

Before we install our CDBReact we have to download the pro version first. Then, we can go ahead to install the react-bootstrap to build the tab component. It is advised to add the file to the root of the project by running.

r
npm install --save ./path-to-the-cdbreact-pro-tgz-file

Or using Yarn

r
yarn add ./path-to-the-cdbreact-pro-tgz-file

Note that we don’t need to install bootstrap or add it anywhere in our project as CDBReact does that for us upon installation.

Our tab would be making use of the Navlink component from React router, so let us install it by running the command below.

Code:

r
npm install react-router-dom

Now run npm start to make sure that everything works well and there are no errors. Before we proceed, let’s go ahead and wrap our app with the BrowserRouter component from react-router-dom as Navlinks can’t work outside it.

Code:

jsx
import './App.css';
import Tab from './tab';
import { BrowserRouter as Router } from 'react-router-dom';
function App() {
return (
<Router>
<div className="App"></div>
</Router>
);
}
export default App;

TAB Component

At this stage, we will have to create a file named tab.js which would contain our Tab component. Import the various tab components that we’ll be using.

Code:

jsx
import React from "react";
import { CDBNav, CDBTabLink, CDBTabContent, CDBTabPane, CDBContainer } from "cdbreact";
import { NavLink } from react-router-dom;

in the file above, we imported some CDBReact for the tab.

  • CDBNav for the navigation of the web.
  • CDBTabLink for links in the tab.
  • CDBTabContent contains the main body of the tab.
  • CDBTabPane contains the pane of the tab.
  • CDBContainer wraps the entire components of the tab.

Code:

jsx
const Tab = () => {
return (
<CDBContainer>
<CDBContainer>
<CDBNav className="nav-tabs mt-5">
<CDBTabLink
link
to="#"
active={this.state.activeItem === '1'}
onClick={this.toggle('1')}
role="tab"
>
Label 1
</CDBTabLink>
<CDBTabLink
link
to="#"
active={this.state.activeItem === '2'}
onClick={this.toggle('2')}
role="tab"
>
Label 2
</CDBTabLink>
<CDBTabLink
link
to="#"
active={this.state.activeItem === '3'}
onClick={this.toggle('3')}
role="tab"
>
Label 3
</CDBTabLink>
</CDBNav>
<CDBTabContent activeItem={this.state.activeItem}>
<CDBTabPane tabId="1" role="tabpanel">
<p className="mt-2">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil odit magnam minima,
soluta doloribus reiciendis molestiae placeat unde eos molestias. Quisquam aperiam,
pariatur. Tempora, placeat ratione porro voluptate odit minima.
</p>
</CDBTabPane>
<CDBTabPane tabId="2" role="tabpanel">
<p className="mt-2">
Quisquam aperiam, pariatur. Tempora, placeat ratione porro voluptate odit minima.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil odit magnam minima,
soluta doloribus reiciendis molestiae placeat unde eos molestias.
</p>
<p>
Quisquam aperiam, pariatur. Tempora, placeat ratione porro voluptate odit minima.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil odit magnam minima,
soluta doloribus reiciendis molestiae placeat unde eos molestias.
</p>
</CDBTabPane>
<CDBTabPane tabId="3" role="tabpanel">
<p className="mt-2">
Quisquam aperiam, pariatur. Tempora, placeat ratione porro voluptate odit minima.
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nihil odit magnam minima,
soluta doloribus reiciendis molestiae placeat unde eos molestias.
</p>
</CDBTabPane>
</CDBTabContent>
</CDBContainer>
</CDBContainer>
);
};
export default Tab;

In the code above, we used CDBNav, CDBTabLink, CDBTabContent, CDBTabPane, CDBContainer to create the tab and add contents with some react-bootstrap styling which beautifies the tab. Let us go ahead to import the created tab component into our app component

Code:

jsx
import './App.css';
import Tab from './tab';
import { BrowserRouter as Router } from 'react-router-dom';
function App() {
return (
<Router>
<div className="App">
<Tab />
</div>
</Router>
);
}
export default App;

The tab component we created will look like the image below

React Bootstrap Tabs

Best Practices for Creating Beautiful React Bootstrap Tabs

When creating beautiful React Bootstrap tabs, keep the following best practices in mind:

  1. Consistent Design: Maintain consistency in tab styling throughout your application to provide a cohesive user experience.

  2. Responsive Design: Test your tabs on different devices and screen sizes to ensure they are fully responsive and function properly.

  3. Accessibility: Ensure that your tabs are accessible to all users by following web accessibility guidelines. Use proper HTML structure and provide descriptive labels for each tab.

Conclusion React Bootstrap tabs are a powerful tool for organizing and presenting content in a user-friendly manner. By following the steps outlined in this article, you can create beautiful and functional tabs that enhance the navigation and visual appeal of your React applications. Experiment with customization options and design choices to create tabs that align with your application's style and purpose. You can also check out some other features you can use in CDBReact Tab Docs. You can also check the code here on our GitHub.

FAQs

  1. Can I use React Bootstrap tabs with my existing React project?

    Yes, you can easily integrate React Bootstrap tabs into your existing React projects by installing the required dependencies and following the implementation steps outlined in this article.

  2. Are React Bootstrap tabs responsive?

    Yes, React Bootstrap tabs are responsive by default. They automatically adjust their layout and behavior based on the screen size and device, ensuring a consistent user experience across different platforms.

  3. Can I customize the appearance of React Bootstrap tabs?

    Absolutely! React Bootstrap provides various customization options for tabs, such as adding custom styles, changing tab positions, applying different colors, and more. Refer to the React Bootstrap Tab documentation for detailed information on available customization options.

  4. Where can I find more resources and examples of React Bootstrap tabs?

The official React Bootstrap 5 documentation offers comprehensive information, examples, and code snippets related to tabs and other components. You can also explore online forums, communities, and tutorials dedicated to React and React Bootstrap for additional resources and inspiration.

Build modern projects using Bootstrap 5 and Contrast

Trying to create components and pages for a web app or website from scratch while maintaining a modern User interface can be very tedious. This is why we created Contrast, to help drastically reduce the amount of time we spend doing that. so we can focus on building some other aspects of the project.

Contrast Bootstrap PRO consists of a Premium UI Kit Library featuring over 10000+ component variants. Which even comes bundled together with its own admin template comprising of 5 admin dashboards and 23+ additional admin and multipurpose pages for building almost any type of website or web app.
See a demo and learn more about Contrast Bootstrap Pro by clicking here.

ad-banner

Related Posts

Comments

...