Skip to content
Advertisement

How to pass key to a cloned component in react using React.cloneElement()

I have a tabs component that accepts the elements array as props and I am trying to assign key to cloned elements by using React.cloneElement() with no luck. I am passing down elements as an array. Mapping over that elements array, cloning each component in that array and trying to assign key to each element.But, I still keep getting the warning about each component should have a key prop. How can I assign keys to each component by using React.cloneElement()?

Below is my elements array that I am passing to my Tabs component.

Below is my tabs component:

JavaScript

Below is the array that I am passing down to that tabs component.

JavaScript

I have also tried the following to try to assign key to each element.

JavaScript

Advertisement

Answer

Problem is in this part of the code

JavaScript

You should also add key prop to each li element

Advertisement