API Reference
useTabs

useTabs

This hook manages the state of a list of tabs, providing the necessary properties for tracking its current state. This hook facilitates adding, removing, and activating tabs within a tabbed interface.

Installation

To use this custom hook in your React project, you can follow these steps:

  1. Make sure you have @ridgehkr/useful installed in your application. If not, you can get started by following the installation instructions.

  2. Import useTabs into your component.

import { useTabs } from '@ridgehkr/useful'

Example

This example demonstrates adding, removing, and switching between tabs:

MyTabs.tsx
import { useTabs } from '@ridgehkr/useful'
 
function MyTabs() {
  const { tabs, activeTab, addTab, removeTab, activateTab } = useTabs()
 
  const handleAddTab = () => {
    addTab(`Tab ${tabs.length + 1}`)
  }
 
  const handleRemoveTab = (index) => {
    removeTab(index)
  }
 
  const handleActivateTab = (index) => {
    activateTab(index)
  }
 
  return (
    <div>
      <ul>
        {tabs.map((tab, index) => (
          <li key={index}>
            <button onClick={() => handleActivateTab(index)}>{tab}</button>
            <button onClick={() => handleRemoveTab(index)}>Remove</button>
          </li>
        ))}
      </ul>
      <button onClick={handleAddTab}>Add Tab</button>
      <p>Active Tab: {tabs[activeTab]}</p>
    </div>
  )
}

API Reference

Hook Signature

useTabs(): TabsState

Parameters

useTabs does not accept any parameters.

Return Value

useTabs returns a TabsState object with the following properties:

PropertyTypeDescription
activeTabnumberThe index of the currently active tab, or -1 if there is no active tab
addTab(tab: string, index?: number) => numberA function to add a new tab to the list. Optionally, specify the index where the new tab should be inserted
removeTab(index: number) => voidA function to remove a tab from the list by its index
activateTab(index: number) => voidA function to set the active tab by its index