score:7

since this question has no answer, i'll just reiterate what's in the comments: when you repeate any element in react (including your custom icon element) those repeated elements must all have key attributes defined.

long explanation about the virtual dom for the curious

the reason for this has to do with react's virtual dom. a simple understanding of react works like this:

  1. react renders your components to the page's dom
  2. events happen, changing state
  3. react re-renders the components that used that state to the dom

but if it was that simple, react would be incredibly wasteful. imagine for instance you have a <ul> ...

<ul>
  <li>item #1</li>
  <li>item #2</li>
  <li>item #3</li>
  <!--... -->
  <li>item #25</li>
</ul>

(presumably you're using a map to generate that in react, but how exactly you make the <ul> is irrelevant.)

now, imagine that you move item #25 to be the first item in the list: if you simply re-rendered everything, you'd have to re-render 25 <li> elements ... even though only one of them changed.

to avoid such unnecessary (and very costly, performance-wise) dom changes, react uses a copy of the dom, known as the virtual dom, and the actual process looks more like:

  1. react renders your components to the page's dom
  2. events happen, changing state
  3. react re-renders the components that used that state to the virtual dom
  4. react does a "diff" of the old and new virtual doms, and only updates the actual dom for the parts that changed in the virtual one

this is a major performance enhancement of using react (vs. using say jquery's .html() method to accomplish the same thing)!

but do you remember when we moved item #25 to #1 a moment ago? from react's perspective, that could have been a move of #25 to #1 ... or it could have been the deletion of #25 and the insertion of a brand new <li> at #1 ... or it could have been a series of updates (of #1 => 25, #2 => #1, #3 => #2, ...)

in other words, react has no way of magically knowing which <li> is which. without a unique identifier on each one, a moved element can look like stuff got deleted/created, or like lots of stuff got updated.

this is where the key prop comes in: whenever you repeat elements (like those <li>s), react has difficulty telling them apart ... but it needs to to be able to use the virtual dom to make your app faster.

this is why it's not required that you add a key to every repeated element (react will still work if you don't) ... but you will get lots of warnings about it ... because it means you're making your app needlessly slower.


Related Query

More Query from same tag