ReactJS – How to add key prop to the Element transmitted as Object in React

Approximately the code looks like this (CoffeeScript)

//In Parent component

render: () ->

mycomp =




//In Child component

render: () ->

someContent = [passedComp, ]



{someContent}

This will generate a warning in the Child component about missing keys in the array.

The question is how to add key={1} to the passComp in the Child component. I can’t do this in the Parent’s render method (with some_prop) because I don’t yet know what the key should be. I need to Add the key prop to the Child component-but what is passed here is already an object.

How to modify passComp to obtain the key?

>Update:

I have

someContent = [{passedComp}, ]

It got rid of the React warning, but the extra span broke my (reaction-bootstrap) CSS. Is there a better solution?

If the component is already instantiated, the only way to do this is to clone your component and add a secret Key attribute

About the code looks like this (CoffeeScript)

//In Parent component

render: () ->

mycomp =




//In Child component

render: () ->

someContent = [passedComp, ]



{someContent}

This will generate a warning in the Child component about missing keys in the array.

p>

The question is how to add key={1} to the passComp in the Child component. I can’t do this in the parent’s render method (with some_prop) because I don’t know what the key should be now. I Need to add the key prop in the Child component-but what is passed here is already an object.

How to modify passComp to obtain the key?

>Update:

I have

someContent = [{passedComp}, ]

It got rid of the React warning, but the extra span broke my (reaction-bootstrap) CSS. Is there a better solution?

If the component is already instantiated, the only way to do this is to clone your component and add the key attribute

Leave a Comment

Your email address will not be published.