Reactstrap is a popular front-end library that is easy to use React Bootstrap 4 components. This library contains the stateless React components for Bootstrap 4. The Tab component allows the user to switch between components present in given different tabs. We can use the following approach in ReactJS to use the ReactJS Reactstrap Tab Component.
TabContent Props:
- activeTab: It is used to denote the ID for the tab which is in an active state.
TabPane Props:
- tabId: It is used to denote the tab ID for the unique identification of each tab.
Creating React Application And Installing Module:
-
Step 1: Create a React application using the following command:
npx create-react-app foldername
-
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
-
Step 3: After creating the ReactJS application, Install the required module using the following command:
npm install reactstrap bootstrap
Project Structure: It will look like the following.
Example 1: Now write down the following code in the App.js file. Here, we have shown a single tab for the user.
App.js
import React from 'react' import 'bootstrap/dist/css/bootstrap.min.css' ; import { TabContent, TabPane, Nav, NavItem, NavLink } from 'reactstrap' ; import classnames from 'classnames' ; function App() { return ( <div style={{ display: 'block' , width: 450, padding: 30 }}> <h4>ReactJS Reactstrap Tab Component</h4> <Nav tabs> <NavItem> <NavLink className={classnames({ active: true })}> Tab Title </NavLink> </NavItem> </Nav> <TabContent activeTab={ '1' }> <TabPane tabId= "1" > <div style={{backgroundColor: 'green' , padding:20}}> Sample Tab Content </div> </TabPane> </TabContent> </div > ); } export default App; |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Example 2: Now write down the following code in the App.js file. Here, we have shown multiple tabs for the user.
App.js
import React, { useState } from 'react' import 'bootstrap/dist/css/bootstrap.min.css' ; import { TabContent, TabPane, Nav, NavItem, NavLink, Row, Col } from 'reactstrap' ; import classnames from 'classnames' ; function App() { // State for current active Tab const [currentActiveTab, setCurrentActiveTab] = useState( '1' ); // Toggle active state for Tab const toggle = tab => { if (currentActiveTab !== tab) setCurrentActiveTab(tab); } return ( <div style={{ display: 'block' , width: 700, padding: 30 }}> <h4>ReactJS Reactstrap Tab Component</h4> <Nav tabs> <NavItem> <NavLink className={classnames({ active: currentActiveTab === '1' })} onClick={() => { toggle( '1' ); }} > Tab1 </NavLink> </NavItem> <NavItem> <NavLink className={classnames({ active: currentActiveTab === '2' })} onClick={() => { toggle( '2' ); }} > Tab2 </NavLink> </NavItem> <NavItem> <NavLink className={classnames({ active: currentActiveTab === '3' })} onClick={() => { toggle( '3' ); }} > Tab3 </NavLink> </NavItem> </Nav> <TabContent activeTab={currentActiveTab}> <TabPane tabId= "1" > <Row> <Col sm= "12" > <h5>Sample Tab 1 Content</h5> </Col> </Row> </TabPane> <TabPane tabId= "2" > <Row> <Col sm= "12" > <h5>Sample Tab 2 Content</h5> </Col> </Row> </TabPane> <TabPane tabId= "3" > <Row> <Col sm= "12" > <h5>Sample Tab 3 Content</h5> </Col> </Row> </TabPane> </TabContent> </div > ); } export default App; |
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Reference: https://reactstrap.github.io/components/tabs/