Open In App

ReactJS Reactstrap Tab Component

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:



TabPane Props:

Creating React Application And Installing Module:



Project Structure: It will look like the following.

Project Structure

Example 1: Now write down the following code in the App.js file. Here, we have shown a single tab for the user.




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.




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:


Article Tags :