Skip to content
Advertisement

Why do Material UI tabs stop working when I use a .map to populate the content dynamically instead of hard coding?

I have implemented Material UI’s tabs successfully by hard-coding the content, but when I tried to make a my hard coded tabs with a .map function to populate the content from a data source (simple json), it no longer works. Can anyone see why? The only change I made was to the MyTabs component below where there are now two .map functions instead of hard coded tabs.

Many thanks for your help!

Here is my data:

JavaScript

Here is my MyTabs component:

JavaScript

And here is the TabsPanel component:

JavaScript

Advertisement

Answer

It doesn’t work because you added some extra Fragments (<> and </>) in the Tabs component and the Tabs component doesn’t accept a Fragment as a child:

If you remove those, it will work as expected:

JavaScript

And please use the key prop with a unique id if you create an array of elements. Each child in a list should have a unique “key” prop.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement