tips on displaying information in a vertical tab orientation

I'm struggling to figure out how to loop inside the <TabPanel> in order to render data using React vertical tabs. I've provided the code I've tried and the data coming from an API. Check out the codesandbox at https://codesandbox.io/s/jovial-darkness-qob1n?file=/src/tab.js

      <Tabs
                defaultTab="vertical-tab-one"
                vertical
                className="vertical-tabs"
              >
                <TabList>
                  {subProducts.map((subProduct, index) => (
                    <Tab>{subProduct.subProductName}</Tab>
                  ))}
                </TabList>
                {subProducts.map((subProduct, index) => (
                  <TabPanel className="tab-pane fade mt-4 show  ">
                    {subProduct.bookingDetails.map((attr, i) => (
                      <>
                        <table id="customers">
                          <tbody>
                            <tr>
                              <td>{attr.name}</td>
                              <td>{attr.value}</td>
                            </tr>
                          </tbody>
                        </table>
                      </>
                    ))}
                  </TabPanel>
                ))}
              </Tabs>

API output:

subProducts: [
        {
          bookingDetails: [
            {
              name: "Birthday Decoration",
              value: "YES"
            },
            {
              name: "Photographer",
              value: "NO"
            }
          ],
          subProductName: "Celebration"
        },
        {
          bookingDetails: [
            {
              name: "Decoration",
              value: "YES"
            },
            {
              name: "Video",
              value: "NO"
            }
          ],
          subProductName: "FamilY"
        }
      ]

Answer №1

While working in the sandbox environment, you encounter an issue when trying to iterate over the bookingDetails within each object of the subProducts array. In the second object of subProducts, there is a Details property instead of a bookingDetails property, resulting in bookingDetails being undefined.

To resolve this, it is recommended to replace Details with bookingDetails:

subProducts: [
  {
    bookingDetails: [
      {
        name: "Birthday Decoration",
        value: "YES",
      },
      {
        name: "Photographer",
        value: "NO",
      },
    ],
    subProductName: "Celebration",
  },
  {
    bookingDetails: [
      {
        name: "Decoration",
        value: "YES",
      },
      {
        name: "Video",
        value: "NO",
      },
    ],
    subProductName: "FamilY",
  },
];

If the API response contains Details instead of

bookingDetails</code as mentioned in your initial query, then you should adjust your code accordingly by mapping over <code>Details
instead:

Therefore, use subProduct.Details.map instead of subProduct.bookingDetails.map.


The issue causing incorrect data display upon clicking is due to each Tab component needing a tabFor prop value that corresponds with a TabPanel's tabId prop value. Without proper alignment, the content won't be displayed correctly.

In this scenario, utilizing the map's index and converting it to a string before passing it as props can serve as a temporary solution. However, it is advisable to have unique identifiers (such as IDs) in your data structure for a more organized approach.

Check out the Example Sandbox here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble installing npx create-react-app with yarn?

When setting up my React app, I initially used npx create-react-app <app-name> with npm, even though I have yarn installed on my computer. The React app was previously installed using yarn. npm version: 8.3.0 node version: 17.3.0 yarn version: 1.22.1 ...

Storing Data in Angular: Tips for Saving Entire Page or Component Data for Future Use

I have developed an Angular 6 application with multiple components that utilize @angular/material's horizontal stepper. Each step within the stepper contains complex forms, some created using template driven forms and others using formgroups. I am loo ...

Issue with localFont not being rendered/applied to the font-sans class in Next.js 13 when using Tailwind CSS

As I embark on a new project with Nextjs and TailwindCSS, I've encountered an issue with getting the fonts to work using variables. In my layout.js: import localFont from "next/font/local"; // Font files can be colocated inside ...

What is the best way to eliminate the dotted border from clickable images?

The client has expressed their dislike for the dotted border, but I believe it is important to keep it for accessibility reasons. However, I suppose I will have to remove it since the client requested it. How should I proceed? ...

Encountering an issue when attempting to update by pressing the button

I am encountering a challenge in my Vue application that involves inserting, updating, and deleting posts using MongoDB. Specifically, I am facing an issue with the update function. Whenever I attempt to update a post by clicking the corresponding button, ...

Improve the efficiency of the function for retrieving data from a lengthy string and organizing it into key-value pairs within an object

I am dealing with a string extracted from a text file, which is structured in the following way on each line. 1=abc|2=TsMarketDataCache|3=1|4=141061|6=2023-02-27T05:04:50.472|36=F|7=1584A Format: key1=value1|key2=value2|key3=value3|key4=value4...|keyN=va ...

"Unexpected behavior: NextAuth is failing to return defined custom scopes

I am currently working on a NextJS project that utilizes NextAuth. Initially, everything was functioning properly with the default scopes. However, my project now requires additional claims, which are listed in the supported scopes here. "scopes_supporte ...

Interacting with individual divisions on a leaflet map

I've been facing a challenge with this interaction for some time now, and finding the appropriate solution seems quite daunting at the moment. Recently, I created a map with basic points using GeoJSON format (generated with PHP & MySQL). I'm cur ...

What is the appropriate title for the css class?

When creating a stylesheet for a website, should the CSS style names be related to the website or its content? For example, if my website is about web development, would it be correct to use style names like: #web-development-header .web-development-comp ...

Different choices for pick component in Bootstrap

Struggling to create an inline label with a select element using Bootstrap, I came across this helpful example on jsfiddle: http://jsfiddle.net/kokilajs/1q53pr6j/ <form id="searchForm" method="get" class="form-horizontal" role="form"> <div class= ...

Upon clicking the button, an event listener is attached to the window

I have an image with an onClick function attached to it. <img className="pointer pipette-icon" src={pipetteIcon} alt="" onClick={handlePipetteClick} /> My objective is as follows: when clicking on this image, nothing should happe ...

Incorporating a dynamic HTML editing button to every new row in a table using JavaScript

My application features a form that allows users to dynamically add new rows to a table using JavaScript: // Function to add a new account row if (tit.innerHTML === "Add Account"){ var table = document.getElementById(tableId); var row = table ...

Using useEffect to pass data from a checkbox element to its parent component

Looking for some React practice, I created a checkbox component to manage a list of devices. My goal is to have the parent element, App, display the name of the device when it's checked. Despite my attempts, I haven't been successful in achieving ...

Embark on a journey through Express.js routes with a unique context

After grappling with this issue for a few days, my brain feels fried and I can't seem to find the most efficient solution. My ultimate goal is to be able to repeat a route journey with new context data at the start. For example: app.get('/test& ...

Issues with arguments not being identified - Discord.js version 14

After discovering that my arguments are no longer being recognized when executing a command, I encountered a strange issue. When args are not included, my console logs undefined, but if args are added, nothing is logged. This is snippet of my command hand ...

Steps for creating an AJAX request to a variable defined within the local scope

I want to create a list using a JSON object that I already have stored in a variable. I have been exploring the dynatable library and its documentation on populating a table using AJAX to receive JSON data. However, I am stuck on how to make it work with ...

Guide to selecting multiple rows at once using ng-options in a list

Need assistance with an HTML list: I have a box displaying a list where users can select multiple items to save. The saving and retrieving process is functional as the selectedList stores the user's previous selections, while fullList contains all av ...

The Bootstrap navbar collapses and vanishes instantly

Whenever I tap on the navbar icon on my mobile or tablet, the navbar opens briefly before disappearing. Even though the navbar closes visually, it still indicates that it is open. When I tap the navbar icon again to close it (without seeing it actually clo ...

What is the reason for not being able to retrieve query parameters in "getStaticProps"?

Let's simplify things: URL: http://localhost:3000/product/bioCloths?activeCategory=medium&id=0 File location: pages/product/[product].js. Expected output: product: "bioCloths, activeCategory: "medium", id: "0" using g ...

Receiving a "Maximum call exceeded" error when using Vue.js router guards for the beforeEach hook

As I work on my Firebase-VueJS app, I am implementing basic security rules with router guards. In my main.js file, I have added the following code to handle permissions based on the user's authentication status. However, I encounter an error 'vue ...