React js Dynamic Tabs add or delete functionality
React js Dynamic Tabs add or delete functionality:Reactjs can implement dynamic tabs with add or delete functionality by managing the tab state using state variables or a state management. When adding a tab, a new tab item is created and added to the tab list. When deleting, the corresponding tab item is removed. The content of each tab is dynamically rendered based on the selected tab. Proper event handling ensures seamless user interaction. Components and their state are updated accordingly, allowing dynamic tab manipulation in Reactjs applications.




Thanks for your feedback!
Your contributions will help us to improve service.
How can I implement dynamic tab addition and deletion functionality in a Reactjs?
This ReactJS code creates a dynamic tab system that allows users to add or delete tabs. The app maintains an array of tabs, where each tab has a heading and content. It uses React hooks, specifically useState, to manage state variables. Users can add a new tab using the "+" Add button and remove the active tab using the "× Remove" button. The active tab's content is displayed while hiding others. Users can edit the heading and content of each tab using input and textarea elements.
React Js Dynamic Tabs Add Or Delete Functionality
xxxxxxxxxx
<script type="text/babel">
const { useState } = React;
function App() {
const [tabs, setTabs] = useState([
{
heading: "Font Awesome Icons",
content: "This is fontawesomeicons.com",
},
{
heading: "Tutorials Plane",
content: "This is tutorialsplane.com",
},
]);
const [activeTabIndex, setActiveTabIndex] = useState(0);
const setActiveTab = (index) => {
setActiveTabIndex(index);
};
const addTab = () => {
const newTab = {
heading: `Tab ${tabs.length + 1}`,
content: "", // Add initial content here
};
setTabs([...tabs, newTab]);
setActiveTabIndex(tabs.length); // Use the functional form to ensure the updated value is correct
};
const removeTab = (index) => {
const updatedTabs = tabs.filter((tab, i) => i !== index);
setTabs(updatedTabs);
setActiveTabIndex(updatedTabs.length ? 0 : null); // Use null instead of 0 when no tabs are left
};
const tabTitle = (index) => {
return `Tab ${index + 1}`;
};
return (
<div className='container'>
<h3>React Dynamic Tab add or delete method</h3>
<div className="tabs">
<ul className="tab-header">
{tabs.map((tab, index) => (
<li
key={index} // Use a unique key for each list item
className={activeTabIndex === index ? "active-tab" : ""}
onClick={() => setActiveTab(index)}
>
{tabTitle(index)}
</li>
))}
<span className="add-remove-icons">
<span className="add-tab-icon" onClick={addTab}>
+ Add
</span>
{activeTabIndex !== null && (
<span
className="remove-tab-icon"
onClick={() => removeTab(activeTabIndex)}
>
× Remove
</span>
)}
</span>
</ul>
<div className="tab-body">
{tabs.map((tab, index) => (
<div className="tab" key={index} style={{ display: activeTabIndex === index ? 'block' : 'none' }}>
<input
value={tab.heading}
onChange={(e) => {
const newTabs = [...tabs];
newTabs[index].heading = e.target.value;
setTabs(newTabs);
}}
/>
<textarea
value={tab.content}
onChange={(e) => {
const newTabs = [...tabs];
newTabs[index].content = e.target.value;
setTabs(newTabs);
}}
></textarea>
</div>
))}
</div>
</div>
</div>
);
}
ReactDOM.render(<App />, document.getElementById("app"));
</script>