What is react-tabs?
The react-tabs package is a simple and flexible tab component for React applications. It allows developers to create tabbed interfaces with ease, providing a straightforward API and customizable components.
What are react-tabs's main functionalities?
Basic Tabs
This feature allows you to create a basic tabbed interface with multiple tabs and corresponding content panels.
import { Tabs, TabList, Tab, TabPanel } from 'react-tabs';
import 'react-tabs/style/react-tabs.css';
function App() {
return (
<Tabs>
<TabList>
<Tab>Tab 1</Tab>
<Tab>Tab 2</Tab>
<Tab>Tab 3</Tab>
</TabList>
<TabPanel>
<h2>Content for Tab 1</h2>
</TabPanel>
<TabPanel>
<h2>Content for Tab 2</h2>
</TabPanel>
<TabPanel>
<h2>Content for Tab 3</h2>
</TabPanel>
</Tabs>
);
}
Custom Styling
This feature demonstrates how to apply custom styling to the tabs by using a custom CSS file.
import { Tabs, TabList, Tab, TabPanel } from 'react-tabs';
import './custom-tabs.css';
function App() {
return (
<Tabs>
<TabList>
<Tab className="custom-tab">Tab 1</Tab>
<Tab className="custom-tab">Tab 2</Tab>
<Tab className="custom-tab">Tab 3</Tab>
</TabList>
<TabPanel>
<h2>Content for Tab 1</h2>
</TabPanel>
<TabPanel>
<h2>Content for Tab 2</h2>
</TabPanel>
<TabPanel>
<h2>Content for Tab 3</h2>
</TabPanel>
</Tabs>
);
}
Dynamic Tabs
This feature shows how to create dynamic tabs that can be added or removed at runtime.
import { useState } from 'react';
import { Tabs, TabList, Tab, TabPanel } from 'react-tabs';
function App() {
const [tabs, setTabs] = useState([
{ title: 'Tab 1', content: 'Content for Tab 1' },
{ title: 'Tab 2', content: 'Content for Tab 2' }
]);
const addTab = () => {
setTabs([...tabs, { title: `Tab ${tabs.length + 1}`, content: `Content for Tab ${tabs.length + 1}` }]);
};
return (
<div>
<button onClick={addTab}>Add Tab</button>
<Tabs>
<TabList>
{tabs.map((tab, index) => (
<Tab key={index}>{tab.title}</Tab>
))}
</TabList>
{tabs.map((tab, index) => (
<TabPanel key={index}>
<h2>{tab.content}</h2>
</TabPanel>
))}
</Tabs>
</div>
);
}
Other packages similar to react-tabs
react-bootstrap
React-Bootstrap is a popular library that provides Bootstrap components as React components. It includes a Tabs component that offers similar functionality to react-tabs but with the added benefit of Bootstrap's styling and theming capabilities.
material-ui
Material-UI is a comprehensive library of React components that implement Google's Material Design. It includes a Tabs component that provides similar functionality to react-tabs, along with a wide range of other UI components and customization options.
semantic-ui-react
Semantic UI React is the official React integration for Semantic UI. It includes a Tab component that offers similar functionality to react-tabs, with the added benefit of Semantic UI's declarative syntax and extensive theming options.
react-tabs
React tabs component
Installing
$ npm install react-tabs
Demo
http://rackt.github.io/react-tabs/example/
Example
var React = require('react');
var ReactDOM = require('react-dom');
var ReactTabs = require('react-tabs');
var Tab = ReactTabs.Tab;
var Tabs = ReactTabs.Tabs;
var TabList = ReactTabs.TabList;
var TabPanel = ReactTabs.TabPanel;
var App = React.createClass({
handleSelect: function (index, last) {
console.log('Selected tab: ' + index + ', Last tab: ' + last);
},
render: function () {
return (
{}
<Tabs
onSelect={this.handleSelected}
selectedIndex={2}
>
{}
<TabList>
{}
<Tab>Foo</Tab>
<Tab>Bar</Tab>
<Tab>Baz</Tab>
</TabList>
{}
<TabPanel>
<h2>Hello from Foo</h2>
</TabPanel>
<TabPanel>
<h2>Hello from Bar</h2>
</TabPanel>
<TabPanel>
<h2>Hello from Baz</h2>
</TabPanel>
</Tabs>
);
}
});
ReactDOM.render(<App/>, document.getElementById('container'));
License
MIT