How to access the “key” property from a reactjs component

The key property is used by React under the hood, and is not exposed to you. You’ll want to use a custom property and pass that data in. I recommend a semantically meaningful property name; key is only to help identify a DOM node during reconciliation, so having another property called listId makes sense.

The key property does not need to be unique for the whole component, but I believe it should be unique for the nesting level you’re in (so generally the loop or collection). If React detects a problem with duplicate keys (in the development build), it will throw an error:

Warning: flattenChildren(…): Encountered two children with the same key, .$a. Child keys must be unique; when two children share a key, only the first child will be used.

Leave a Comment